Functions 2: Savitch ch.
Call by Reference Functions
What is a Reference?
• A reference is the memory address of a variable.
• So, In this type of functions memory locations of
variable are communicated between functions rather
than the variables values.
Call Comparisons
Call By Reference vs Value Slide 5- 3
• Call-by-reference • Call-by-value
Memory • The function
• The function call: call:
Name Location Contents
f(age); f(age);
age 1001 34
initial 1002 A
hours 1003 23.5
1004
• void f(int& ref_par); void f(int val_par);
This means
that:
1. Same memory location is shared by the two functions
( the CALLING function and the CALLED function).
2. Each of these functions refer to the shared memory
location by a local variable name of its own.
3- The consequence of this fact is that if a function
change the value of this shared variable location,
it will be also, changed in the other function.
Features of Call-by-Reference
Functions:
• The type of the function is “void”, since NO
value
would be Returned through the function name.
• The use of “return” is optional, since the
function ends automatically at the end of its
statements.
• The function parameter-list could contain a mix
Features of Call-by-Reference
Functions:
• In the parameter list the type of reference
parameter is followed by the
symbol”&”(Ambersand,
the ‘and’ Example:
void func1(int x, int& y, float numbers[ ])
Type Value parameter reference Array Reference by default
• The Array is passed as reference by default.
What is passed to the function really is just the
Visualizing
Visualizing passing
passing by reference:
by reference: Memory of main
function
// Example: Function to swap two numbers N1 N2
7.5 35.0
#include <iostream>
#include <iomanip> 35.0 7.5
using namespace std;
void swap(float&, float&) ; // prototype of the function swap
void main()
{
float N1= 7.5, N2=35.0 ;
cout<< “Before calling the function “ << setw(10)<< N1 << setw(10)<< N2 <<endl;
Before calling the function 7.5
swap( N1, N2); 35.0
cout<< “After calling the function “ << setw(10)<< N1 << setw(10)<< N2 <<endl;
}
After calling the function 35.0
void swap(float& X, float& Y) 7.5
{ float Temp;
* * 7.5
Temp=X ;
X =Y; Memory of swap
X Y Temp function
Y=Temp; }
Function to calculate sum of salaries and their
average:
#include <iostream>
#include <iomanip>
#include<string>
using namespace std;
void Sum_Av (int , float [ ], float&, float&) ; // prototype of the function Sum_Av
int main()
{
const int array_size = 100; // array_size is the Max. possible no. of
employees
int N; // N is actual number of employees
string name[array_size]; //array of names
float S[array_size], sum, ave; // salaries, sum of salaries, and average of
salaries
cout<< “Inter No. of employees “ ;
cout<< “inter employee name and his salary”<< endl;
for (in i-0; i<N; i++)
{
cin >> name[i] >> S[i];
cout << left << setw(30)<< name[i] << setw(10) << S[i]<< endl;
}
Cout<< “----------------------------------------------------------------------------------”<< endl;
Sum_Av(N , S, sum, ave);
cout << “ sum of Salaries =“<< setw(10) << sum << setw(10) << ave <<endl;
cout<< “----------------------------------------------------------------------------------”<< endl;
}
void Sum_Av (int Number , float Salary[ ], float& S, float& V)
{
S=0.0;
for (int i=0; i< Number; i++)
S = S + Salary[i];
V = S / Number;