Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5 functions 1 ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Similar presentations


Presentation on theme: "Lecture 5 functions 1 ©1992-2012 by Pearson Education, Inc. All Rights Reserved."— Presentation transcript:

1 Lecture 5 functions 1 ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

2 6.5 Function Prototypes and Argument Coercion A function prototype (also called a function declaration) tells the compiler ▫ the name of a function ▫ the type of data returned by the function ▫ the number of parameters the function expects to receive ▫ the types of those parameters and ▫ the order in which the parameters of those types are expected. 2 ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

3 3

4 6.5 Function Prototypes and Argument Coercion(cont.) The compiler refers to the function prototype to check that calls to function contain the correct number and types of arguments and that the types of the arguments are in the correct order. In addition, the compiler uses the prototype to ensure that the value returned by the function can be used correctly in the expression that called the function (e.g., a function call that returns void cannot be used as the right side of an assignment statement). Each argument must be consistent with the type of the corresponding parameter. If the arguments passed to a function do not match the types specified in the function’s prototype, the compiler attempts to convert the arguments to those types. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. 4

5 6.6 Functions with Empty Parameter Lists In C++, an empty parameter list is specified by writing either void or nothing at all in parentheses. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. 5

6 6

7 7

8 8

9 6.7 Function control There are three ways to return control to the point at which a function was invoked. If the function does not return a result (i.e., it has a void return type), control returns when the program reaches the function-ending right brace, or by execution of the statement  return; If the function does return a result, the statement  return expression ; evaluates expression and returns the value of expression to the caller. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. 9

10 6.17 Function Overloading C++ enables several functions of the same name to be defined, as long as they have different signatures. This is called function overloading. The C++ compiler selects the proper function to call by examining the number, types and order of the arguments in the call. Function overloading is used to create sev-eral functions of the same name that perform similar tasks, but on different data types. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. 10

11 ©1992-2012 by Pearson Education, Inc. All Rights Reserved. 11

12 ©1992-2012 by Pearson Education, Inc. All Rights Reserved. 12

13 7.5 Passing Arrays to Functions To pass an array argument to a function, specify the name of the array without any brack-ets. When passing an array to a function, the array size is normally passed as well, so the function can process the specific number of elements in the array. C++ passes arrays to functions by reference—the called functions can modify the element values in the callers’ original arrays. The value of the name of the array is the address in the computer’s memory of the first element of the array. ▫ Because the start-ing address of the array is passed, the called function knows precisely where the array is stored in memory. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. 13

14 Example: passing array to function Write C++ program using function to calculate the sum of array 14 ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

15

16

17

18


Download ppt "Lecture 5 functions 1 ©1992-2012 by Pearson Education, Inc. All Rights Reserved."

Similar presentations


Ads by Google