Вы находитесь на странице: 1из 7

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

BACHELOR OF BUSINESS ADMINISTRATION


(INFORMATION TECHNOLOGY.)
“Mittal School of Business”

LOVELY PROFESSIONAL UNIVERSITY


Phagwara, Punjab.
INTRODUCTION:
Function:
We often write large programs and it is a good idea to split large steps into smaller procedures.
These small procedure blocks are known as functions. Functions are often required to do
repetitive jobs. We can define a function and call them from anywhere we need. This is a good
choice for code reuse and code size optimization.

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.

Difference between Call by Value and Call by Reference:


Example of call by value:
Basic program to demonstrate:

Output:

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.
2nd Program of call by value-Swapping of two number:

Output

In the above program it is about swapping two number by using call by value
method.
Example of Call by reference Method:
Basic program to demonstrate:

Output:

In the above code passed an argument to first variable and change the value of the
variable by passing argument.
2nd program to demonstrate call by reference method:

Output:

in the following program variables are swapped using references.

Вам также может понравиться