Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views9 pages

8 - Functions 2 Call by Reference

The document explains call-by-reference functions, highlighting that they share the same memory location between the calling and called functions, allowing changes in one to reflect in the other. It details the features of such functions, including the use of the '&' symbol for reference parameters and the default behavior of arrays being passed by reference. Additionally, it provides examples of functions for swapping numbers and calculating the sum and average of salaries.

Uploaded by

Omgt Hfuu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

8 - Functions 2 Call by Reference

The document explains call-by-reference functions, highlighting that they share the same memory location between the calling and called functions, allowing changes in one to reflect in the other. It details the features of such functions, including the use of the '&' symbol for reference parameters and the default behavior of arrays being passed by reference. Additionally, it provides examples of functions for swapping numbers and calculating the sum and average of salaries.

Uploaded by

Omgt Hfuu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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;

You might also like