Call by Val Ref
Call by Val Ref
In C, all function arguments are passed "by value" because C does not
support references like C++ and Java do. In C, the calling and called
functions do not share any memory -- they have their own copy and the
called function cannot directly alter a variable in the calling function; it
can only alter its private, temporary copy.
#include <stdio.h>
OUTPUT
======
n1: 10, n2: 20
The swapByValue() does not affect the arguments n1 and n2 in the calling
function it only operates on a and b local to swapByValue() itself. This is a
good example of how local variables behave.
Example using Call by Reference
In call by reference, to pass a variable n as a reference parameter, the
programmer must pass a pointer to n instead of n itself. The formal
parameter will be a pointer to the value of interest. The calling function
will need to use & to compute the pointer of actual parameter. The called
function will need to dereference the pointer with *where appropriate to
access the value of interest. Here is an example of a
correctswap swapByReference() function. So, now you got the difference
between call by value and call by reference!
#include <stdio.h>
OUTPUT
======
n1: 20, n2: 10