Function Part-1
Function Part-1
)
OBJECTIVES
In this chapter, you will:
Learn how to construct and use void functions in a program
Discover the difference between value and reference parameters
Explore reference parameters and value-returning functions
Learn about the scope of an identifier
2
OBJECTIVES (CONTINUED)
Examine the difference between local and global
identifiers
Discover static variables
Learn function overloading
Explore functions with default parameters
3
VOID FUNCTIONS
4
VOID FUNCTIONS (CONTINUED)
A void function does not have a return type
return statement without any value is typically used to exit the
function early
Formal parameters are optional
A call to a void function is a stand-alone statement
5
VOID FUNCTIONS WITHOUT PARAMETERS
Function definition syntax:
6
VOID FUNCTIONS WITH PARAMETERS
Function definition syntax:
7
VOID FUNCTIONS WITH PARAMETERS
(CONTINUED)
8
VOID FUNCTIONS WITH PARAMETERS
(CONTINUED)
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
9
VALUE PARAMETERS
If a formal parameter is a value parameter
The value of the corresponding actual parameter is copied into it
The value parameter has its own copy of the data
During program execution
The value parameter manipulates the data stored in its own
memory space
10
REFERENCE VARIABLES AS PARAMETERS
11
REFERENCE VARIABLES AS PARAMETERS
(CONTINUED)
Reference parameters can:
Pass one or more values from a function
Change the value of the actual parameter
Reference parameters are useful in three situations:
Returning more than one value
Changing the actual parameter
When passing the address would save memory space and time
12
CALCULATE GRADE
13
CALCULATE GRADE (CONTINUED)
14
VALUE AND REFERENCE PARAMETERS AND
MEMORY ALLOCATION
16
VALUE AND REFERENCE PARAMETERS AND
MEMORY ALLOCATION (CONTINUED)
17
REFERENCE PARAMETERS AND VALUE-
RETURNING FUNCTIONS
25
SCOPE OF AN IDENTIFIER
The scope of an identifier refers to where in the program an identifier is accessible
Local identifier: identifiers declared within a function (or block)
Global identifier: identifiers declared outside of every function definition
C++ does not allow nested functions
The definition of one function cannot be included in the body of another function
26
SCOPE OF AN IDENTIFIER (CONTINUED)
Some compilers initialize global variables to default values
The operator :: is called the scope resolution operator
By using the scope resolution operator
A global variable declared before the definition of a function
(block) can be accessed by the function (or block) even if the
function (or block) has an identifier with the same name as the
variable
28
SCOPE OF AN IDENTIFIER (CONTINUED)
C++ provides a way to access a global variable declared after the
definition of a function
In this case, the function must not contain any identifier with the
same name as the global variable
29
GLOBAL VARIABLES, NAMED CONSTANTS, AND
SIDE EFFECTS
Using global variables has side effects
A function that uses global variables is not independent
If more than one function uses the same global variable and something goes wrong
It is difficult to find what went wrong and where
Problems caused in one area of the program may appear to be from another area
Global named constants have no side effects
30
STATIC AND AUTOMATIC VARIABLES
Automatic variable: memory is allocated at block entry and deallocated
at block exit
By default, variables declared within a block are automatic variables
Static variable: memory remains allocated as long as the program
executes
Variables declared outside of any block are static variables
Declare a static variable within a block by using the reserved word
static
31
STATIC AND AUTOMATIC VARIABLES
(CONTINUED)
The syntax for declaring a static variable is:
The statement
static int x;
declares x to be a static variable of the type int
Static variables declared within a block are local to the block
Their scope is the same as any other local identifier of that block
32
FUNCTION OVERLOADING: AN INTRODUCTION
In a C++ program, several functions can have the same name
This is called function overloading or overloading a function name
33
FUNCTION OVERLOADING (CONTINUED)
Two functions are said to have different formal parameter lists if
both functions have:
A different number of formal parameters, or
If the number of formal parameters is the same, then the data type
of the formal parameters, in the order you list them, must differ in
at least one position
34
FUNCTION OVERLOADING (CONTINUED)
The following functions all have different formal parameter lists:
35
FUNCTION OVERLOADING (CONTINUED)
Function overloading: creating several functions with the same name
The signature of a function consists of the function name and its formal parameter
list
Two functions have different signatures if they have either different names or different
formal parameter lists
Note that the signature of a function does not include the return type of the function
36
FUNCTION OVERLOADING (CONTINUED)
Correct function overloading:
Syntax error:
37
FUNCTIONS WITH DEFAULT PARAMETERS
In a function call, the number of actual and formal parameters must be the
same
C++ relaxes this condition for functions with default parameters
You specify the value of a default parameter when the function name
appears for the first time (e.g., in the prototype)
If you do not specify the value of a default parameter, the default value is
used
38
FUNCTIONS WITH DEFAULT PARAMETERS
(CONTINUED)
39
FUNCTIONS WITH DEFAULT PARAMETERS
(CONTINUED)
Consider the following prototype:
Assume:
a, b are int, ch is char, d is double
Examples of legal calls:
40
FUNCTIONS WITH DEFAULT PARAMETERS
(CONTINUED)
Examples of illegal function prototypes with default parameters:
41
PROGRAMMING EXAMPLE: CLASSIFY NUMBERS
In this example, we use functions to rewrite the program that determines the number of
odds and evens from a given list of integers
Main algorithm remains the same:
Initialize variables, zeros, odds, evens to 0
Read a number
If number is even, increment the even count
If number is also zero, increment the zero count; else
increment the odd count
Repeat Steps 2-3 for each number in the list
42
PROGRAMMING EXAMPLE: CLASSIFY NUMBERS
(CONTINUED)
The program functions include:
initialize: initialize the variables, such as zeros, odds, and evens
getNumber: get the number
classifyNumber: determine if number is odd or even (and whether it is also
zero); this function also increments the appropriate count
printResults: print the results
43
PROGRAMMING EXAMPLE: MAIN ALGORITHM
Call initialize to initialize variables
Prompt the user to enter 20 numbers
For each number in the list
Call getNumber to read a number
Output the number
Call classifyNumber to classify the number and increment the appropriate
count
Call printResults to print the final results
45
SUMMARY
47
SUMMARY (CONTINUED)
48
SUMMARY (CONTINUED)
Variables declared within a function (or block) are called local variables
Variables declared outside of every function definition (and block) are global variables
Automatic variable: variable for which memory is allocated on function/block entry and
deallocated on function/block exit
Static variable: memory remains allocated throughout the execution of the program
C++ functions can have default parameters
49