Objected Oriented Programming
Objected Oriented Programming
CAP282
Topic: Call by Reference And call by value
Submitted by:
JAMEEL AHMAD
Registration no.: 11813086
In partial fulfillment for the requirements of the award of the degree of
There are two ways to pass value or data to function in C language: call by value and call by
reference. Original value is not modified in call by value but it is modified in call by reference.
Call by value:
In call by value mechanism, the called function creates a new set of variables in stack and copies
the values of the arguments into them.
Call by value method copies the value of an argument into the formal parameter of that function.
Therefore, changes made to the parameter of the main function do not affect the argument.
In this parameter passing method, values of actual parameters are copied to function's formal
parameters, and the parameters are stored in different memory locations. So, any changes made
inside functions are not reflected in actual parameters of the caller.
Call by Reference:
In call by reference mechanism, instead of passing values to the function being called,
references/pointers to the original variables are passed.
Call by reference method copies the address of an argument into the formal parameter. In this
method, the address is used to access the actual argument used in the function call. It means that
changes made in the parameter alter the passing argument.
In this method, the memory allocation is the same as the actual parameters. All the operation in
the function are performed on the value stored at the address of the actual parameter, and the
modified value will be stored at the same address.
In the given code first, variable value is given as 5, but after that we pass the
second value as 3 so, it printed as 3.
In the above program it is about swapping two number by using call by value
method.
Output:
In the above code passed an argument to first variable and change the value of the
variable by passing argument.