C++ Learning Resource Network!

 C++ Tutorials  >  Functions  >  Call by Value

Call by Value

In the Call by Value method, the called function creates new variables to store the value of the arguments passed to it.

The following program illustrates the invoking of a function by value:

//Program 7.4
//This function swaps the value of two variable
void swap(int, int);
void main()
int iVar1, iVar2;
cout<<"Input two numbers "<<endl;
swap(iVar1, iVar2);
cout<<"In main "<<iVar1<<" "<<iVar2<<endl;
void swap(int iNum1, int iNum2)
int iTemp;
iTemp = iNum1;
iNum1 = iNum2;
iNum2 = iTemp;
cout<<"In swap "<<iNum1<<" "<<iNum2<<endl;

The sample output of Program 7.4 is:

Input two numbers
In swap 25 15
In main 15 25

In Program 7.4, values entered for the variables iVar1 and iVar2 are passed to the function swap(). When the function swap() is invoked, these values get copied into the memory locations of the parameters iNum1 and iNum2, respectively.

This is depicted in the following figures(refer Figure 7.1 and 7.2).


iVar1   iVar2     iVar1   iVar2  

















iNum1   iNum2     iNum1   iNum2  
Fig 7.1 When swap() Funtion is invokded   Fig 7.2 After the swap() Function is Executed

Therefore, the variables in the calling function main() are distinct from the variables in the called function swap() as they occupy distinct memory locations.

In Program 7.4, the function arguments are passed by value. When arguments are passed by value, the called function creates new variables of the same data type as the arguments passed to it. The values of these arguments are copied into the newly created variables. Passing arguments by value is useful when the function does not need to modify the values of the original variables in the calling program. Therefore, the values of the variables in the calling functions do not get affected when the arguments are passed as values.

Reference Variable

A reference variable provides an alias- an alternate name- for the variable. A reference variable is declared by preceding the variable name with an ampersand (&). The following statements declare a reference variable called refer.

int num;
int &refer = num;

In the statements given above, refer is a reference variable or alias to the variable num. A reference declaration allows a variable name and the reference name to be used interchangeably. Both the variable and its reference share the same memory address.

The following program illustrates the use of reference variables:

//Program 7.5
//This program illustrates the use of reference variables
void main()
int number = 5;
int &ref = number;
cout<<"Number is "<<number<<endl;
cout<<"Incresing the number......"<<endl;
cout<"Number now is "<<number<<endl;
cout<<"Reference now is "<<ref<<endl;
cout<<"Number now is "<<number<<endl;

In Program 7.5, the statement


increments the value of the variable number by 1, the value of the variable number therefore becomes 6. The statement:


again increments the value of the variable number by 1 because ref points to the same memory location as number. The output of program 7.5 is:

Number is 5
Increasing the number........
NUmber now is 6
Reference now is 7
Number now is 7

A reference variable can be initialized only when it is declared.


| Bookmark This Site |   | Links |   | Search |   | Submit |   | Contact |

  Copyright 2008 - 3008  CppTutor.com All rights reserved.