Call By Value:
Creates a new memory loaction for use within the
subroutine.The memory is freed once it leaves the
subroutine.Changes made to the variable are not affected
outside the subroutine.
Call By Reference:
Passes a pointer to the memory location.Changes made to the
variable within the subroutine affects the variable outside
the subroutine.
..............................................................
Call by value method:
passing the value of variable to the function.
void main()
{
int x=10,y=20;
printf("%d%d',x,y);
swap(x,y);
}
void swap(int a,int b)
{
int c;
c=a;//changes here do not affect in values
a=b;//of x and y in main function..
b=c;
}
call by reference method:
passing the address of variable to the function.
swap(&a,&b)
&c=&a;
&a=&b;
&b=&c;
changes made in subfunction causes changes in address of
variables n thus in main () also..
............................................................
call by value :
call by value means programmer send some value coping from
one function to another.At the time of function calling a
programmer can send a copy of variable of value.
call by referance :
it means sending sending the address of variable to the
called function means a user can send the address of
variable.
2 comments:
Good answer. Additionally check this page. It explains with a flow diagram.
call by value and call by reference with example
Post a Comment