Copyright © Programs ++
Design by Dzignine
Friday 20 January 2012

Call by value

In call by value, the copy of the argument is passed in the parameter list i.e any changes made will not affect the original values of the arguments. Its is useful when the original value of the arguments is to be retained. An example is given below :  

// Program to show call by value

#include<iostream.h>

void swapArgs(int a,int b)
{
    int temp; // temp. variable to store the value of a
    temp = a;
    a = b;
    b = temp;
    cout<<" Value of x and y after swap : "<<a<<" "<<b;
} // end of swapArgs function

int main()
{
    int x = 5;
    int y = 10;
    swapArgs(x,y); // calls function to swap the values
    cout<<" Value of x and y before swap : "<<x<<" "<<y;
    /* as u can see the original values are not swapped as the original values of
       x and y are not passed,only their copies are passed as arguments */
    return 0;
} // end of main









------ Related Posts ------

Call by reference : http://programsplusplus.blogspot.in/2012/01/8-call-by-reference.html

0 comments:

Post a Comment

Comment Here....