Chapter 6 Functions
Chapter 6 Functions
FUNCTIONS
6.1
Modular Programming
Modular Programming
Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection of statements to perform a task Motivation for modular programming:
Improves maintainability of programs Simplifies the process of writing programs
6-3
6-4
6.2
Defining and Calling Functions
6-6
Function Definition
Definition includes:
return type: data type of the value that function returns to the part of the program that called it name: name of the function. Function names follow same rules as variables parameter list: variables containing values passed to the function body: statements that perform the functions task, enclosed in {}
6-7
Function Definition
Note: The line that reads int main() is the function header.
6-8
6-9
Calling a Function
To call a function, use the function name followed by () and ;
printHeading();
When called, program executes the body of the called function After the function terminates, execution resumes in the calling function at point of call.
6-10
6-11
6-12
Calling Functions
main can call any number of functions Functions can call other functions Compiler must know the following about a function before it is called:
name return type number of parameters data type of each parameter
6-13
6.3
Function Prototypes
Function Prototypes
Ways to notify the compiler about a function before a call to the function:
Place function definition before calling functions definition
Use a function prototype (function declaration) like the function definition without the body
Header: void printHeading() Prototype: void printHeading();
6-15
(Program Continues)
6-16
6-17
Prototype Notes
Place prototypes near top of program
Program must include either prototype or full function definition before any call to the function compiler error otherwise When using prototypes, can place function definitions in any order in source file
6-18
6.4
Sending Data into a Function
Values passed to function are arguments Variables in a function that hold the values passed as arguments are parameters
6-20
The integer variable num is a parameter. It accepts any integer value passed to the function.
6-21
(Program Continues)
6-22
6-23
The function call in line 11 passes the value 5 as an argument to the function.
6-24
6-25
6-26
6-28
(Program Continues)
6-29
6-30
The function call in line 18 passes value1, value2, and value3 as a arguments to the function.
6-31
6.5
Passing Data by Value
6-33
val 5
argument in calling function
num 5
parameter in evenOrOdd function
evenOrOdd can change variable num, but it will have no effect on variable val
6-34
6.6
Using Functions in Menu-Driven Programs
6-36
6.7
The return Statement
Can be used to prevent abnormal termination of program In a void function without a return statement, the function ends at its last }
6-38
(Program Continues)
6-39
Program 6-11(Continued)
6-40
6.8
Returning a Value From a Function
6-42
6-43
A Value-Returning Function
Return Type
int sum(int num1, int num2) { double result; result = num1 + num2; return result; }
A Value-Returning Function
int sum(int num1, int num2) { return num1 + num2; }
6-45
(Program Continues)
6-46
6-47
The statement in line 17 calls the sum function, passing value1 and value2 as arguments. The return value is assigned to the total variable.
6-48
6-49
6-50
6.9
Returning a Boolean Value
6-52
(Program Continues)
6-53
6-54
6.10
Local and Global Variables
6-56
6-57
When the program is executing in main, the num variable defined in main is visible. When anotherFunction is called, however, only variables defined inside it are visible, so the num variable in main is hidden.
6-58
This means that any value stored in a local variable is lost between calls to the function in which the variable is declared.
6-59
6-60
6-61
Global constants defined for values that do not change throughout the programs execution.
6-62
The constants are then used for those values throughout the program.
6-63
6.11
Static Local Variables
(Program Continues)
6-67
In this program, each time showLocal is called, the localNum variable is re-created and initialized with the value 5.
6-68
(Program Continues)
6-69
statNum is automatically initialized to 0. Notice that it retains its value between function calls.
6-70
If you do initialize a local static variable, the initialization only happens once. See Program 6-23.
6-71
6.12
Default Arguments
Default Arguments
A Default argument is an argument that is passed automatically to a parameter if the argument is missing on the function call. Must be a constant declared in prototype:
void evenOrOdd(int = 0);
Can be declared in header if no prototype Multi-parameter functions may have default arguments for some or all of them:
int getSum(int, int=0, int=0);
6-73
(Program Continues)
6-74
6-75
Default Arguments
If not all parameters to a function have default values, the defaultless ones are declared first in the parameter list:
int getSum(int, int=0, int=0);// OK int getSum(int, int=0, int); // NO
When an argument is omitted from a function call, all arguments after it must also be omitted:
sum = getSum(num1, num2); sum = getSum(num1, , num3); // OK // NO
6-76
6.13
Using Reference Variables as Parameters
6-78
Passing by Reference
A reference variable is an alias for another variable Defined with an ampersand (&)
void getDimensions(int&, int&);
Changes to a reference variable are made to the variable it refers to Use reference variables to implement passing parameters by reference
6-79
The & here in the prototype indicates that the parameter is a reference variable.
(Program Continues)
6-80
6-81
6-82
6.14
Overloading Functions
Overloading Functions
Overloaded functions have the same name but different parameter lists Can be used to create functions that perform the same task but take different parameter types or different number of parameters Compiler will determine which version of function to call by argument and parameter lists
6-84
int length, width; double base, height; getDimensions(length); getDimensions(length, width); getDimensions(length, height); getDimensions(height, base);
// // // //
1 2 3 4
6-85
Passing a double
Passing an int
(Program Continues)
6-86
6-87
6.15
The exit() Function
6-89
6.16
Stubs and Drivers