DDC 2133 Programming II
1
Objectives
To create functions, invoke functions, and pass arguments to a
function (§5.2-5.4).
To understand the differences between pass-by-value and passby-reference (§§5.5-5.6).
To use function overloading and understand ambiguous
overloading (§5.7).
To use function prototypes for declaring function headers (§5.8).
To create header files for reusing functions (§5.11).
To determine the scope of local and global variables (§5.13).
To develop applications using the C++ mathematical functions
(§5.14).
To design and implement functions using stepwise refinement
(§5.15).
DDC 2133 Programming II
2
Introducing Functions
A function is a collection of statements that are
grouped together to perform an operation.
Invoke a funciton
Define a function
return value type
function
header
method name
formal parameters
int max(int num1, int num2)
{
function
body
int result;
parameter list
if (num1 > num2)
result = num1;
else
result = num2;
int z = max(x, y);
actual parameters
(arguments)
return value
return result;
}
DDC 2133 Programming II
3
Introducing Functions, cont.
• Function signature is the combination of the
function name and the parameter list.
• The variables defined in the function header are
known as formal parameters.
• When a function is invoked, you pass a value to
the parameter. This value is referred to as actual
parameter or argument.
DDC 2133 Programming II
4
Introducing Functions (cont.)
• A Function may return a value. The
returnValueType is the data type of the value the
function returns. If the function does not return a
value, the returnValueType is the keyword void.
DDC 2133 Programming II
5
Calling Functions
Listing 5.1 Testing the max Function
This program demonstrates calling a Function
max to return the largest of the int values
TestMax
Run
DDC 2133 Programming II
6
animation
Calling Functions (cont.)
pass the value i
pass the value j
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
return result;
}
DDC 2133 Programming II
7
animation
Trace Function Invocation
i is now 5
pass the value i
pass the value j
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
return result;
}
DDC 2133 Programming II
8
animation
Trace Function Invocation
j is now 2
pass the value i
pass the value j
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
return result;
}
DDC 2133 Programming II
9
animation
Trace Function Invocation
invoke max(i, j)
pass the value i
pass the value j
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
return result;
}
DDC 2133 Programming II
10
animation
Trace Function Invocation
invoke max(i, j)
Pass the value of i to num1
Pass the value of j to num2
pass the value i
pass the value j
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
return result;
}
DDC 2133 Programming II
11
animation
Trace Function Invocation
declare variable result
pass the value i
pass the value j
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
return result;
}
DDC 2133 Programming II
12
animation
Trace Function Invocation
(num1 > num2) is true since num1
is 5 and num2 is 2
pass the value i
pass the value j
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
return result;
}
DDC 2133 Programming II
13
animation
Trace Function Invocation
result is now 5
pass the value i
pass the value j
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
return result;
}
DDC 2133 Programming II
14
animation
Trace Function Invocation
return result, which is 5
pass the value i
pass the value j
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
return result;
}
DDC 2133 Programming II
15
animation
Trace Function Invocation
return max(i, j) and assign the
return value to k
pass the value i
pass the value j
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
return result;
}
DDC 2133 Programming II
16
animation
Trace Function Invocation
Execute the print statement
pass the value i
pass the value j
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
return result;
}
DDC 2133 Programming II
17
Call Stacks
Space required for the
max function
result: 5
num2: 2
num1: 5
Space required for the
main function
k:
j: 2
i: 5
(a) The main function
is invoked.
Space required for the
main funciton
k:
j: 2
i: 5
Space required for the
main function
k: 5
j: 2
i: 5
(b) The max
function is invoked.
(c) The max function is
finished and the return
value is sent to k.
DDC 2133 Programming II
Stack is empty
(d) The main function
is finished.
18
animation
Trace Call Stack
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
i is declared and initialized
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
i: 5
The main method
is invoked.
return result;
}
DDC 2133 Programming II
19
animation
Trace Call Stack
j is declared and initialized
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
j: 2
int max(int num1, int num2)
i: 5
{
int result; The main method
is invoked.
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
DDC 2133 Programming II
20
animation
Trace Call Stack
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
Declare k
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
Space required for the
main method
k:
j: 2
i: 5
int max(int num1, int num2)
{
int result;
The main method
is invoked.
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
DDC 2133 Programming II
21
animation
Trace Call Stack
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
Invoke max(i, j)
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
Space required for the
main method
k:
j: 2
i: 5
int max(int num1, int num2)
{
int result;
The main method
is invoked.
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
DDC 2133 Programming II
22
animation
Trace Call Stack
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
pass the values of i and j to num1
and num2
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
num2: 2
num1: 5
Space required for the
main method
k:
j: 2
i: 5
int max(int num1, int num2)
{
int result;
The max method is
invoked.
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
DDC 2133 Programming II
23
animation
Trace Call Stack
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
}
(num1 > num2) is true
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
result:
num2: 2
num1: 5
Space required for the
main method
k:
j: 2
i: 5
int max(int num1, int num2)
{
int result;
The max method is
invoked.
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
DDC 2133 Programming II
24
animation
Trace Call Stack
Assign num1 to result
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
cout << "The maximum between "
<< i << " and " + j + " is "
Space required for the
<< k;
max method
result: 5
return 0;
num2: 2
num1: 5
}
Space required for the
main method
k:
j: 2
i: 5
int max(int num1, int num2)
{
int result;
The max method is
invoked.
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
DDC 2133 Programming II
25
animation
Trace Call Stack
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
Return result and assign it to k
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
Space required for the
return 0; max method
result: 5
num2: 2
num1: 5
}
Space required for the
main method
k:5
j: 2
i: 5
int max(int num1, int num2)
{
int result;
The max method is
invoked.
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
DDC 2133 Programming II
26
animation
Trace Call Stack
int main()
{
int i = 5;
int j = 2;
int k = max(i, j);
Execute print statement
cout << "The maximum between "
<< i << " and " + j + " is "
<< k;
return 0;
}
Space required for the
main method
k:5
j: 2
i: 5
int max(int num1, int num2)
{
int result;
The main method
is invoked.
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
DDC 2133 Programming II
27
void Functions
The preceding section gives an example of a
nonvoid function. This section shows how to
declare and invoke a void function. Listing 5.2
gives a program that declares a function named
printGrade and invokes it to print the grade for a
given score.
TestVoidFunction
DDC 2133 Programming II
Run
28
© Copyright 2026 Paperzz