Chapter 5 - Functions
How to create a user-defined function?
5.4 FUNCTION ELEMENTS
• Function Definition
• Function Prototype
• Function Call
5.4.3 Function Call
▪ Function call is used to access the function definition.
▪ The function call can be placed within:
▪ main() function
▪ user-defined function
▪ to call other user-defined function or
▪ to call itself (recursion)
Function Call (cont’)
There are two ways to call/invoke the function:
1. Pass-by-value
2. Pass-by-reference
5.4.3 Function Call (cont’) – Pass by value
General Syntax:
(a) use the following format for value-returning function.
variableName = functionName (list of actual parameters);
Example 6:
(pass-by-value at first line of function definition)
int findMin (int a, int b)
(pass-by-value at the calling statement)
y = findMin (a, b);
5.4.3 Function Call (cont’) – Pass by value
General Syntax (cont’)
(b) use the following format for void function
functionName (list of actual parameters);
Example 7:
(pass-by-value at first line of function definition for the void function)
void view (int x)
(pass-by-value at the calling statement)
view (x);
5.4.3 Function Call (cont’) – Pass by reference
General Syntax (cont’)
(b) use the following format for void function
functionName (list of actual parameters);
Example 8:
(pass-by-reference at first line of function definition for the void function)
void view (int &x);
(pass-by-reference at the calling statement)
view (x);
5.4.3 Function Call (cont’)
Pass-by-value
▪ The value holds by actual parameter is copied to the formal parameter.
▪ If the variable within the function is modified then upon return from the
function, the value of actual variable is not modified.
Pass-by-reference
▪ The address of actual parameter is copied to the formal parameter.
▪ If the variable within the function is modified then the value of actual
variable is modified.
#include <iostream>
using namespace std; (pass-by-value)
/*---------------------------
The increment() function
---------------------------*/
void increment (int n)
{
n++;
cout<<"Inside the function, n is "<<n<<endl;
}
/*---------------------------
The main() function
---------------------------*/
int main()
{ Output:
int x=1;
cout << "Before the call, x is " << x << endl;
increment (x);
cout << " After the call, x is " << x << endl;
return 0;
}
#include <iostream> (pass-by-value)
using namespace std;
/*--------------------------- Memory snapshot
The increment() function
---------------------------*/ Memory address Variable name Content
void increment (int n) 0x1ft0 n 1
{
n++;
cout<< "Inside the function, n is "<< n<< endl;
}
/*---------------------------
The main() function
---------------------------*/
int main()
{
int x=1; 0x7ft4 x 1
cout << "Before the call, the content of x is " << x << endl;
increment (x);
cout << " After the call, the content of x is " << x << endl;
return 0;
}
#include <iostream> (pass-by-value)
using namespace std;
/*--------------------------- Memory snapshot
The increment() function
---------------------------*/ Memory address Variable name Content
void increment (int n)
{ 0x1ft0 n 2
n++;
cout<< "Inside the function, n is "<< n<< endl;
}
/*---------------------------
The main() function
---------------------------*/
int main()
{
int x=1; 0x7ft4 x 1
cout << "Before the call, the content of x is " << x << endl;
increment (x);
cout << " After the call, the content of x is " << x << endl;
return 0;
}
#include <iostream> (pass-by-reference)
using namespace std;
/*--------------------------- Memory snapshot
The increment() function
---------------------------*/ Memory address Variable name Content
void increment (int &n) 0x3ft0 ? 0x7ft4
{
n++;
cout<< "Inside the function, n is "<< n<< endl;
}
/*---------------------------
The main() function
---------------------------*/
int main()
{
int x=1; 0x7ft4 x 1
cout << "Before the call, the content of x is " << x << endl;
increment (x);
cout << " After the call, the content of x is " << x << endl; Output
return 0;
}
#include <iostream> (pass-by-reference)
using namespace std;
/*--------------------------- Memory snapshot
The increment() function
---------------------------*/ Memory address Variable name Content
void increment (int &n)
{ 0x3ft0 ? 0x7ft4
n++;
cout<< "Inside the function, n is "<< n<< endl;
}
/*---------------------------
The main() function
---------------------------*/
int main()
{
int x=1; 0x7ft4 x 2
cout << "Before the call, the content of x is " << x << endl;
increment (x);
cout << " After the call, the content of x is " << x << endl; Output
return 0;
}
Arrays & functions
Arrays & Functions
▪ Arrays are always passed by reference.
▪ The “[ ]” in the formal parameter specification indicates that the variable is
an array.
▪ It is a good practice to pass the dimension of the array as another
parameter.
▪ If the function must not change any element of the array then const should
be used in the formal parameter specification of that array.
▪ Programmer can pass array elements OR the whole array to the function
//Example 8:passing array elements to a user-defined function
#include <iostream> Passing element
using namespace std; by element of
array
void print_square (int);
const int ARRAY_SIZE = 5;
/*----------------------
The main()
-------------------------*/
int main(){
int index;
int base[ARRAY_SIZE] = {3, 7, 2, 4, 5};
for(index = 0; index < ARRAY_SIZE; index++)
print_square(base[index]); //call statements to pass array elements to the user-defined function
cout << endl;
return 0;
/*----------------------
The print_square() receives array elements
-------------------------*/
void print_square(int number)
cout << " " << number * number;
}
//Example 9: passing the whole array to a user-defined function
#include <iostream>
using namespace std;
Passing whole
double average (int, const int[]);
array
/*----------------------
The main()
-------------------------*/
int main(){
const int array_size = 5;
double ave;
int base[array_size] = {3, 7, 2, 4, 5};
ave = average(array_size, base); //call statements to the whole array to the user-defined function
cout << "The average of the numbers ";
for (int index = 0; index < array_size;index++)
cout << base[index] <<“,”;
cout << " is " << ave << endl;
return 0;
/*----------------------
The average() receives the whole array at once
-------------------------*/
double average( int size, const int inp_list[]) {
double sum = 0.0;
for ( int index = 0; index < size; index++)
sum += inp_list[index];
return sum/size;
}
Summary
Function elements:
1. Function Definition
2. Function Prototype
3. Function Call
Summary of Function Call
1. Function call calls the function definition.
2. There are two ways of function call;
1. call by value (pass by value)
2. call by reference (pass by reference)
3. Working with arrays and functions
1. call by value – pass element by element to the function
2. Call by reference – pass the whole array to the function