Chapter 6: User-Defined Functions
Objectives
In this chapter, you will: Learn about standard (predefined) functions Learn about user-defined functions Examine value-returning functions Construct and use a value-returning, user-defined function Construct and use void functions Understand value and reference parameters
2
Introduction
Functions are often called modules They are like miniature programs that can be combined to form larger programs They allow complicated programs to be divided into manageable pieces
Predefined Functions
In C++, a function is similar to that of a function in algebra
It has a name It does some computation
Some of the predefined mathematical functions are:
sqrt(x) pow(x, y) floor(x)
5
Predefined Functions (cont'd.)
Predefined functions are organized into separate libraries
I/O functions are in iostream header Math functions are in cmath header
To use predefined functions, you must include the header file using an include statement See Table 6-1 in the text for some common predefined functions
6
User-Defined Functions
Value-returning functions: have a return type
Return a value of a specific data type using the return statement
Void functions: do not have a return type
Do not use a return statement to return a value
Value-Returning Functions
To use these functions, you must:
Include the appropriate header file in your program using the include statement Know the following items:
Name of the function Number of parameters, if any Data type of each parameter Data type of the value returned: called the type of the function
Value-Returning Functions (contd.)
Can use the value returned by a valuereturning function by:
Saving it for further calculation Using it in some calculation Printing it
A value-returning function is used in an assignment or in an output statement
Heading (or function header): first line of the function
Example: int abs(int number)
Value-Returning Functions (contd.)
Formal parameter: variable declared in the heading
Example: number
Actual parameter: variable or expression listed in a call to a function
Example: x = pow(u, v)
10
Syntax: Value-Returning Function
Syntax:
functionType is also called the data type or return type
11
Syntax: Formal Parameter List
12
Function Call
Syntax to call a value-returning function:
13
Syntax: Actual Parameter List
Syntax of the actual parameter list:
Formal parameter list can be empty: A call to a value-returning function with an empty formal parameter list is:
14
return Statement
Function returns its value via the return statement
It passes this value outside the function
15
Syntax: return Statement
Syntax: In C++, return is a reserved word When a return statement executes
Function immediately terminates Control goes back to the caller
When a return statement executes in the function main, the program terminates
16
Syntax: return Statement (contd.)
17
Function Prototype
Function prototype: function heading without the body of the function
Syntax: Not necessary to specify the variable name in the parameter list Data type of each parameter must be specified
18
Value-Returning Functions: Some Peculiarities
19
Value-Returning Functions: Some Peculiarities (contd.)
20
Flow of Execution
Execution always begins at the first statement in the function main Other functions are executed only when called Function prototypes appear before any function definition
Compiler translates these first
Compiler can then correctly translate a function call
21
Flow of Execution (contd.)
Function call transfers control to the first statement in the body of the called function When the end of a called function is executed, control is passed back to the point immediately following the function call
Functions returned value replaces the function call statement
22
Void Functions
User-defined void functions can be placed either before or after the function main If user-defined void functions are placed after the function main
The function prototype must be placed before the function main
Void function does not have a return type
return statement without any value is typically used to exit the function early
23
Void Functions (contd.)
Formal parameters are optional A call to a void function is a stand-alone statement Void function definition syntax:
C++ Programming: From Problem Analysis to Program Design, Sixth Edition
24
Void Functions (contd.)
Formal parameter list syntax: Function call syntax:
Actual parameter list syntax:
25
Void Functions (contd.)
Value parameter: a formal parameter that receives a copy of the content of corresponding actual parameter Reference parameter: a formal parameter that receives the location (memory address) of the corresponding actual parameter
26
Value Parameters
If a formal parameter is a value parameter:
The value of the corresponding actual parameter is copied into it Formal parameter has its own copy of the data
During program execution
Formal parameter manipulates the data stored in its own memory space
27