the called function creates a new set of variables in stack is called call by value whch is copies
the values of the arguments into method.
void swap(int x, int y) {
int z;
z = x;
x = y;
y = z;
printf("Swapped values are a = %d and b = %d", x, y);
}
int main (int argc, char *argv[])
{
int a = 10, b = 6;
printf("Original values are a = %d and b = %d", a, b);
swap(a, b);
printf("The values after swap are a = %d and b = %d", a, b);
}
- push value of b
- push value of a
- save return address
- call function
Call by reference:
instead of passing values to the function by a pointers to the original variables are passed this method known as call by reference method.
void swap(int *x, int *y) {
int z;
z = *x;
*x = *y;
*y = z;
printf("Swapped values are a = %d and b = %d", *x, *y);
}
int main (int argc, char *argv[])
{
int a = 10, b = 6;
printf("Original values are a = %d and b = %d", a, b);
swap(&a, &b);
printf("The values after swap are a = %d and b = %d", a, b);
}
- push address of b
- push address of a
- save return address
- call function
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Call by value:
the called function creates a new set of variables in stack is called call by value whch is copies the values of the arguments into method.
- push value of b
- push value of a
- save return address
- call function
Call by reference:
instead of passing values to the function by a pointers to the original variables are passed this method known as call by reference method.
- push address of b
- push address of a
- save return address
- call function