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
6
Creating Void Functions (cont)
7
Example - Void Functions With No Parameters #include using namespace std; void display( );
//prototype declaration
int main ( ) { int num1=5,num2; display( );
//function call
num2=num1*num1; cout<
Result :
} void display( );
// function definition
{ cout<<“*****Welcome******\n” ; }
*****Welcome** **** 5 * 5 = 25
8
Example - Void Functions With Parameters
#include 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 { cout<
Result : Enter two integers: 5 10 5 * 10 = 50
9
Passing Variables to a Function
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++
11
Passing Variables By Value (cont)
12
Passing Variables By Value (cont)
13
Passing Variables By Value (cont)
14
Passing Variables By Reference
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
19
Passing Variables By Value and 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 * 4!
5 * 4! 4 * 3!
5!=5*24=120 is returned 4!=4*6=24 is returned 4 * 3!
3 * 2!
3!=3*2=6 is returned 3 * 2!
2 * 1!
2!=2*1=2 is returned
2 * 1! 1
a) Procession of recursive call
1 is returned
1 b) Values returned from each recursive call
Programming Example 26
#include #include using namespace std; unsigned long factorial(unsigned long); int main( ) { for (int i=0; i<= 10; i++) cout << setw(2)<<<“!=“<< factorial(i)<<endl; return 0; } //Recursive definition of function factorial unsigned long factorial( unsigned long number) { if(number <=1) return 1; else return number *factorial(number – 1); }
Output : 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800 Calculating factorials with a 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