Can anyone explain me the diffrence between call by reference and a call by value
Can anyone explain me the diffrence between call by reference and a call by value
see for yourself.... compile and run this:-
Code:#include <stdio.h> #include<stdlib.h> void byvalue (int x) { x+=5; // x=x+5 printf("Value of x in byvalue is %d\n",x); } void byref(int*x) { (*x)+=5; printf("Value of x in byref is %d\n",(*x)); } int main() { int x=5; int* y=&x; byvalue(x); printf("x in main is %d\n",x); byref(y); printf("x in main is %d\n",x); system("PAUSE"); return 0; }
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
a call by value passes only the-value-of a variable, while a call by reference passes a reference (address usually) to a variable so that it's value can be altered
Passing by reference is faster than passing by value (and it saves a little bit of memory), but when passing by value, you can't accidently change the value of a variable.