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
how can we enable hyperlink in screen painter?
in bdc session method. if u run the record in fore ground manually i have a 7 records but at the time of record processing first record produces the error how can u process records manually in fore ground please tell me any one knows?
Is it possible to pass data to and from include programs explicitly? : abap modularization
Difference between function group and function module?
Kindly help me to Know the process of mapping in EDI from R/3 to a convertor(third party which translates IDoc flatfile to EDIFACT /XML / FTP /HTTP ) ?
What is an abap dictionary? : abap data dictionary
What is the function of the correction system? : abap data dictionary
Explain buffering
What is the differences between abap and ooabap. In which situation we useooabap?
Can you define a field without a data element? : abap data dictionary
Can we define our own match code id's for sap matchcodes? : abap data dictionary
What is a maintenance view? : abap data dictionary
Give Some examples of reports generated in real time scenarion? Also give examples of standard reports in SAP used in real time?
Name some system global variables you can use in abap programs?
What are the differences between domain and data element?