wat is difference between parameter passing by reference and parameter passing by value? and example?
wat is difference between parameter passing by reference and parameter passing by value? and example?
Parameters passing by reference means we are directly passing the memory address where value is saved.
Parameters passing by value means we are passing the parameter value only.
check this link for a better example.
http://www.cs.princeton.edu/~lworthi...s_val_ref.html
Last edited by jainbrijesh; 04-24-2007 at 09:03 AM.
Regards,
Brijesh Jain
---------------------------------------------------------
Connect with me on Skype: jainbrijesh
Google Plus : jainbrijeshji
Parameter passing by value means only a value is passed to the function and any changes to the parameters doesn’t change the original arguments passed to the function.
Eg
main()
{
int a=10,b=20,c=0;
c=func(a,b); // pass by value
print a // prints 10
print b // prints 20
};
func(int x,int y) // recieves values 10 & 20
{
x=x+1; // adds 1 to x
y=y+1; // adds 1 to y
print x; // prints 11
print y; // ptints 21
}
Parameter passing by reference means the address of the original arguments are passed to the function and any changes to the parameters change the original arguments also.
Eg
main()
{
int a=10,b=20,c=0;
c=func(&a,&b); // pass by reference
print a // prints 11
print b // ptints 21
};
func(int *x,int *y) // recieves references to a & b
{
*x=*x+1; // adds 1 to a
*y=*y+1; // adds 1 to b
print *x; // prints a as 11
print *y; // prints b as 21
}
Lack of WILL POWER has caused more failure than
lack of INTELLIGENCE or ABILITY.
-sutnarcha-
Excellent example !!! Its good one :-)