Function Part2
Function Part2
LECTURE 7
Objectives
2
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
}
Recursive Function
24
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