FUNCTION (part 2)
LECTURE 7
Objectives
2
Create a function that does not return a value
Invoke a function that does not return a value
Pass information, by reference, to a function
Use void functions in a .NET C++ program
Concept Lesson
3
More About Functions
Creating Void Functions
Passing Variables to a Function
Passing Variables By Value and By Reference
More About Functions
4
Use functions to avoid duplicating code
They also allow large and complex programs to be
broken into small and manageable tasks
main() is typically responsible for invoking each
of the other functions when needed
Program-defined functions streamline main()
Functions can be:
Value-returning
Void
Creating Void Functions
5
Void functions do not return a value after
completing their assigned tasks
E.g., to display information (such as a title and column
headings) at the top of each page in a report
Avoids repeating code several times in program
Creating Void Functions (cont)
6
Example - Void Functions With No
7
Parameters
#include <iostream>
using namespace std;
void display( ); //prototype declaration
int main ( )
{
int num1=5,num2;
display( ); //function call
num2=num1*num1;
cout<<num1<<“ * ”<<num1<<“ = “<<num2;
return 0;
Result :
}
void display( ); // function definition *****Welcome******
{ 5 * 5 = 25
cout<<“*****Welcome******\n” ;
}
Example - Void Functions With
8
Parameters
#include <iostream>
using namespace std;
void display(int, int, int); //prototype declarations
int main ( )
{
int number1,number2,number3;
cout << “Enter two integers:”;
cin >> number1>> number2;
number3 = number1*number2;
display(number1,number2,number3); //function call
return 0;
}
void display(int num1,int num2,int num3); // function definition Result :
{ Enter two integers:
cout<<num1 <<“ * “<< num2<<“ = “<<num3;
5 10
}
5 * 10 = 50
Passing Variables to a Function
9
A variable has both a value and a unique address
(memory location)
You can pass either the variable’s value or its
address to the receiving function
Pass by value
Pass by reference
Passing Variables By Value
10
In a pass by value, the computer passes the contents
of the variable to the receiving function
Receiving function is not given access to the variable
in memory
It cannot change value stored inside variable
By default, variables are passed by value in C++
Passing Variables By Value (cont)
11
Passing Variables By Value (cont)
12
Passing Variables By Value (cont)
13
Passing Variables By Reference
14
Passing a variable’s address is referred to as
passing by reference
Receiving function has access to passed variable
Use when you want receiving function to change
contents of variable
To pass a variable by reference in C++
Include an & before the name of the corresponding
formal parameter in receiving function’s header
Called the address-of operator
Passing Variables By Reference (cont)
15
Passing Variables By Reference (cont)
16
Passing Variables By Reference (cont)
17
pass by value vs. pass by reference
18
Pass by value
a copy of data is created and placed in a local variable in the
called function
ensure that regardless of how the data is manipulated and
changed in the called function, the original data in the calling
function are safe and unchanged
Pass by reference
sends the address of a variable to the called function
use the address operator (&) in the parameter of the called
function
anytime we refer to the parameter, therefore we actually referring
to the original variable
if the data is manipulated and changed in the called function, the
original data in the calling function are changed
Passing Variables By Value and
19
By Reference
You can mix the way variables are passed when a
function is called, passing some by reference and
others by value
Program in Figure 10-15 allows user to enter an
employee’s current salary and raise rate
It then calculates and displays the employee’s raise and
new salary amounts
Passing Variables By Value and
By Reference (cont)
20
Passing Variables By Value and
By Reference (cont)
21
Recursive Function
22
A function that calls itself, either directly or indirectly
(through another function).
The format of recursive function consists of two parts
that are simple problem and complex problem.
For simple problem, it returns a value directly.
However, for complex problem the function will divide
the problem into two parts, which are (i) the part that
can be done directly and (ii) the part that call itself
recursively or repeatedly.
Recursive Function
23
Recursive function calling is done before the initial
calling ends.
The base format of recursive function
type function_name(parameter list)
{
if(simple problem case)
solve it
else
redefine the problem by calling the
function recursively;
}
Recursive Function
24
For example the calculation of factorial or ‘n!’ in
mathematics. Refer to the example below.
unsigned long factorial(unsigned long number)
{
if (number <= 1)
return 1; //simple problem case
else
return number * factorial(number–1);
//complex problem case
}
Recursive Function
25
The diagram below shows the calculation flow of calling
and returning values in factorial() for problem of 5!
Final value=120
5! 5!
5!=5*24=120 is returned
5 * 4! 5 * 4!
4!=4*6=24 is returned
4 * 3! 4 * 3!
3!=3*2=6 is returned
3 * 2! 3 * 2!
2!=2*1=2 is returned
2 * 1! 2 * 1!
1 is returned
1 1
a) Procession of recursive b) Values returned from each recursive call
call
Programming Example
26
#include <iostream> Output :
#include <iomanip>
using namespace std; 0! = 1
1! = 1
unsigned long factorial(unsigned long);
2! = 2
int main( ) 3! = 6
{
for (int i=0; i<= 10; i++) 4! = 24
cout << setw(2)<<i<<“!=“<< factorial(i)<<endl; 5! = 120
return 0; 6! = 720
} 7! = 5040
//Recursive definition of function factorial
unsigned long factorial( unsigned long number) 8! = 40320
{ 9! = 362880
if(number <=1)
return 1; 10! = 3628800
else Calculating factorials with a
return number *factorial(number – 1); recursive function
}
Summary
27
Functions can be:
Value-returning
Void
Function header begins with void
Pass by value: value stored inside variable is passed
to receiving function
Pass by reference: variable’s address in memory is
passed to receiving function
Receiving function can change variable’s contents
Use & before corresponding formal parameter