Home          welcome : Guest          Log In      
  Xpode.com Beta version

Click here to Search Article

Categories/Articles

 

Function - Call by value

 

Submitted By:


Rohit kakria

Detail : I am working as software developer in a company. I love to enjoy life. I think "Life is that what we think".

In this method the values of the actual arguments are copied to the formal arguments. Since the formal arguments are only the copies of the actual arguments, if we do any changes in the formal arguments they do not affect the actual arguments.

For example (C++):

        void swap(int,int);

void main ( )
{
int a,b;
clrscr ( );
a=10;
b=20;
cout<<"Before calling swap function: a="<<a<<" b="<<b;
swap (a,b);
cout<<"\nAfter calling swap function: a="<<a<<" b="<<b;
getch ( );
}

void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"\nInside swap function: x="<<x<<" y="<<y;
}


Here in the main ( ) function we have declared two variables a and b with values 10 and 20 respectively. So we have in memory:

Call By value

When we call the swap ( ) function by value, two new variables x and y are created with separate memory locations from a and b and the values of a and b are copied to x and y respectively. So we have in memory now:

Call By value

Inside the swap ( ) function we have interchanged the values of x and y. Since x and y are separate memory locations from a and b, any changes made to x and y will not affect a and b. So after swap function we have in memory:

Call By value

Click here for example







Comments
Share this article   |    Print   
Guest      Thursday, January 21, 2010
QMoc3z  &lt;a href=&quothttp://dpolxxfwqhtj.com/&;quot;&gt;dpolxxfwqhtj&lt;/a&gt;, yjtprpirfnoz, [link=http://tonqqcpysesq.com/]tonqqcpysesq[/link], http://byizmaqazhih.com/
Guest      Thursday, January 21, 2010
QMoc3z  &lt;a href=&quothttp://dpolxxfwqhtj.com/&;quot;&gt;dpolxxfwqhtj&lt;/a&gt;, yjtprpirfnoz, [link=http://tonqqcpysesq.com/]tonqqcpysesq[/link], http://byizmaqazhih.com/
 
*  Name         *   Comments :ar 8) :cry: 8)
:shock: :shock: :evil: :!:
:( :idea: :lol: :x
:green: :| :question: :P
:oops: :roll: :) :o
:twisted: :wink:
*  Email          
                          (will not publish)
     
  Location           
  Website            
Enter the code *
(as shown below)