Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Similar documents
Scope and Parameter Passing

Minimal Spanning Trees

The Critical-Path Algorithm

Scope and Parameter Passing

Scheduling and Digraphs

The Pairwise-Comparison Method

The Class Construct Part 1

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Sampling Distribution Examples Sections 15.4, 15.5

Programming Languages

The Class Construct Part 2

Rotations and Translations

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Ambient and Diffuse Light

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Implementing Linked Lists

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Dynamic Allocation of Memory

Recursive Linked Lists

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The Decreasing-Time Algorithm

Recursive Descent Parsers

The Traveling Salesman Problem Brute Force Method

Friends and Unary Operators

Street-Routing Problems

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Solving Recursive Sequences by Iteration

LR Parsing - Conflicts

Programming Languages

Recognition of Tokens

Binary Tree Applications

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Stacks and their Applications

The Traveling Salesman Problem Nearest-Neighbor Algorithm

The Graphics Pipeline

Dynamic Allocation of Memory

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

List Iterator Implementation

The Graphics Pipeline

Magnification and Minification

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

The CYK Parsing Algorithm

The x86 Instruction Set

JFlex. Lecture 16 Section 3.5, JFlex Manual. Robb T. Koether. Hampden-Sydney College. Mon, Feb 23, 2015

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Density Curves Sections

Fundamental Data Types

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

The Plurality-with-Elimination Method

Calling Prewritten Functions in C

Introduction to Compiler Design

Inheritance: The Fundamental Functions

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Displaying Distributions - Quantitative Variables

MySQL Creating a Database Lecture 3

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

DTDs and XML Attributes

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

The x86 Architecture

Introduction to Databases

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Binary Tree Implementation

Binary Tree Implementation

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

Building the Abstract Syntax Trees

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

The Coefficient of Determination

XQuery FLOWR Expressions Lecture 35

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

The Projection Matrix

Nondeterministic Programming in C++

Review. Lecture 39. Robb T. Koether. Mon, Dec 5, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) Review Mon, Dec 5, / 14

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

Function Definition Syntax Tree

Inheritance: The Fundamental Functions

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Functional Dependencies and Normal Forms

The Mesh Class. Lecture 23. Robb T. Koether. Hampden-Sydney College. Wed, Oct 18, 2017

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Functional Dependencies and Normal Forms

C Programs: Simple Statements and Expressions

PHP Queries and HTML Forms Lecture 23

Abstract Syntax Trees Synthetic and Inherited Attributes

Transcription:

Function Usage Lecture 15 Sections 6.3, 6.4 Robb T. Koether Hampden-Sydney College Mon, Oct 1, 2018 Robb T. Koether (Hampden-Sydney College) Function Usage Mon, Oct 1, 2018 1 / 12

1 Function and Parameters 2 The Function Interface 3 Header Files 4 Assignment Robb T. Koether (Hampden-Sydney College) Function Usage Mon, Oct 1, 2018 2 / 12

Functions and Parameters A function is like a program within a program. Every function has a name. rand() sqrt() pow() A function may have any number of parameters, including none, always written within parentheses. rand() sqrt(x) pow(x, 5) Robb T. Koether (Hampden-Sydney College) Function Usage Mon, Oct 1, 2018 3 / 12

Return Values Return Values double r = sqrt(x); int n = floor(y); srand(time(0)); A function may return a value of a specified type. If it does not return a value, then it is a void function. Robb T. Koether (Hampden-Sydney College) Function Usage Mon, Oct 1, 2018 4 / 12

Function Usage Function Usage b = a * sin(angleb) / sin(anglea); c = sqrt(pow(a, 2) + pow(b, 2)); // Law of Sines // Pythagorean Thm A function reference may be used anywhere that an object of its return type is permitted. If a function is void, then it must be used on a line by itself. The parameters of a function call may themselves be function calls. Robb T. Koether (Hampden-Sydney College) Function Usage Mon, Oct 1, 2018 5 / 12

Function Usage Function Usage srand(static_cast<unsigned int>(time(0))); If a function is void, then it must be used on a line by itself. Robb T. Koether (Hampden-Sydney College) Function Usage Mon, Oct 1, 2018 6 / 12

Function Interfaces A function interface contains Exactly the information needed by the programmer in order to use the function correctly. Exactly the information needed by the compiler in order to check the usage of the function. Robb T. Koether (Hampden-Sydney College) Function Usage Mon, Oct 1, 2018 7 / 12

Function Interfaces The function interface specifies The function name The return type The number of parameters The types of the parameters The order of the parameters Which parameters are constant (if any) The method of parameter passing (value or reference) Whether the return value is constant The method of passing back the return value (value or reference) Which parameters have default values (if any) It does not need to specify the names of the parameters. Robb T. Koether (Hampden-Sydney College) Function Usage Mon, Oct 1, 2018 8 / 12

Function Prototypes int rand(void); double sqrt(double); double pow(double, double); double pow(double, int); void srand(unsigned int); A function prototype is C++ s method of specifying a function interface. The same function name may have several different prototypes; they represent different functions of the same name. Visit the web site www.cplusplus.com/reference for more details. Robb T. Koether (Hampden-Sydney College) Function Usage Mon, Oct 1, 2018 9 / 12

Function Prototypes The function prototype must appear before the function is used. Typically, the function prototypes are put in a header file and included using #include. The benefit of this is that we may write the prototype only once (in the header file) and then include it in as many programs as we wish. Robb T. Koether (Hampden-Sydney College) Function Usage Mon, Oct 1, 2018 10 / 12

Example: Header Files Example HeaderFileExample.cpp Robb T. Koether (Hampden-Sydney College) Function Usage Mon, Oct 1, 2018 11 / 12

Assignment Assignment Read Sections 6.3, 6.4. Robb T. Koether (Hampden-Sydney College) Function Usage Mon, Oct 1, 2018 12 / 12