Difference Between Call By Value and Call By Reference?

Answer Posted / shashi shekhar

For instance consider program1

main()
{
int x=50, y=70;
interchange(x,y);
printf(“x=%d y=%d”,x,y);
}

interchange(x1,y1)
int x1,y1;
{
int z1;
z1=x1;
x1=y1;
y1=z1;
printf(“x1=%d y1=%d”,x1,y1);
}

Here the value to function interchange is passed by value.

Consider program2

main()
{
int x=50, y=70;
interchange(&x,&y);
printf(“x=%d y=%d”,x,y);
}

interchange(x1,y1)
int *x1,*y1;
{
int z1;
z1=*x1;
*x1=*y1;
*y1=z1;
printf(“*x=%d *y=%d”,x1,y1);
}

Here the function is called by reference. In other words
address is passed by using symbol & and the value is
accessed by using symbol *.

The main difference between them can be seen by analyzing
the output of program1 and program2.

The output of program1 that is call by value is
x1=70 y1=50
x=50 y=70

But the output of program2 that is call by reference is

*x=70 *y=50
x=70 y=50

This is because in case of call by value the value is passed
to function named as interchange and there the value got
interchanged and got printed as

x1=70 y1=50

and again since no values are returned back and therefore
original values of x and y as in main function namely

x=50 y=70 got printed.

But in case of call by reference address of the variable got
passed and therefore what ever changes that happened in
function interchange got reflected in the address location
and therefore the got reflected in original function call in
main also without explicit return value. So value got
printed as *x=70 *y=50 and x=70 y=50

Is This Answer Correct ?    8 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between retroactive accounting period, earliest retroactive accounting period? : sap abap hr

539


How to pass data from list to report?

560


What are the types of search helps? : sap abap data dictionary

628


What are pool tables?

640


What is the use of the raising exception?

558






How many lists can a program can produce?

582


What is a 'z' report?

638


What should be the approach for writing a bdc program? : abap bdc

571


what next in SAP-ABAP/4 is it going to be change?

1640


In delivery processing which step comes first picking, packing, posting goods issue ?

1619


Explain about the tables exists in a data dictionary and what are they? : abap data dictionary

621


Explain client-dependent and client-independent tables.

599


What are the difference between tables and structures? : abap data dictionary

640


How many types of size categories and data classes are there? : abap data dictionary

591


What is meant by authorization? : sap abap hr

544