1,473 views
1 votes
1 votes
what are the differences between call by Reference and Call by Address in different programming languages

1 Answer

3 votes
3 votes

Call by value in C

In call by value, value being passed to the function is locally stored by the function parameter in stack memory location.
If you change the value of function parameter, it is changed for the current function only.
It will not change the value of variable inside the caller method such as main().

Consider a simple program :

void swap(int x, int y);

int main () {


int a = 100;
int b = 200;

printf("Before swap, value of a : %d\n", a );//Before swap, value of a :100
printf("Before swap, value of b : %d\n", b );//Before swap, value of b :200

swap(a, b);

printf("After swap, value of a : %d\n", a );//Before swap, value of a :100
printf("After swap, value of b : %d\n", b );//Before swap, value of b :200
return 0;
}

void swap(int x, int y) {

int temp;
temp = x;
x = y;
return;
}




Call by reference in C

In call by reference, original value is modified because we pass reference (address).

Here, address of the value is passed in the function, so actual and formal arguments shares the same address space.
Hence, value changed inside the function, is reflected inside as well as outside the function.

Consider a simple program :

void swap(int *x, int *y);

int main () {


 int a = 100;
 int b = 200;

printf("Before swap, value of a : %d\n", a ); // Before swap, value of a :100
printf("Before swap, value of b : %d\n", b );//  Before swap, value of b :200

swap(&a, &b);

printf("After swap, value of a : %d\n", a );//Before swap, value of a :200
 printf("After swap, value of b : %d\n", b );//Before swap, value of b :100

 return 0;
}

void swap(int *x, int *y) {

int temp;
temp = *x;
 *x = *y;

return;
}

PS:C has only pass by value. The second case here is pass by pointer- which is also pass by value as the value of the pointer is being copied and passed as a value. We get the effect of pass by reference because we use "*" operator explicitly inside the function to get the content of the pointed location. This is different from pass by reference where this is done entirely by the language and not the programmer.

edited by

Related questions

3 votes
3 votes
1 answer
1
Himanshu Goyal asked Aug 27, 2016
628 views
Is call by address in C same to call by reference in C++? if not then whats basic difference ?? and why doesn't C have a refernce variable just like C++??
1 votes
1 votes
1 answer
2
neha singh asked Aug 26, 2016
928 views
Program P1() { x=10, y=3; func1(y,x,x); print x; print y; } func1(x,y,z) { y=y+4; z=x+y+4; }plzz elaborate the answer explanation?thanks in advance.
0 votes
0 votes
1 answer
3
shree asked Nov 22, 2014
519 views
What will be the second value printed by the program if parameter passing mechanism is call by reference?int b=10 //global begin procedure func(int x,int y) begin print(b...
0 votes
0 votes
0 answers
4
sushmita asked Jan 18, 2017
880 views
foo(a, b, c) { b =b++; a = a++; c = a + b*10 } X=1; Y=2; Z=3; foo(X, Y+2, Z);CAN SOMEONE PLEASE TELL THE OUTPUTS FOR CALL BY REFERENCE AND CALL BY NAME. WHAT IS THE DIFER...