Assume that you don\'t know what particular parameter passing style a programmin
ID: 3803188 • Letter: A
Question
Assume that you don't know what particular parameter passing style a programming language is using, hi order to find out., you are asked to write a short test program that will print a different output depending on whether a call-by-value, cat-by-reference, or call-by-value-result parameter passing style is used. Your test program must have the following form: program main() {a integer; procedure foo(integer x) {//statement "body of foo}//statement body of main a = 1; call foo(a); print a;} The body of procedure foo must only contain assignment statements. For instance, you are not. allowed to add any new variable declarations. Write the body of procedure foo such that print a in the main program will print different values for the different, parameter passing styles. Give the output of your test program and explain why your solution works.Explanation / Answer
call by value:-
#include <stdio.h>
int foo(int x)
{
return x;
}
int main() {
int a=1;
foo(a);
printf("value of a is: %d",a);
return(0);
}
in call by value:-
- when we pass the parameters using call by value , xerox copy of original parameter is created and passed to the called function.
- Any update made inside method will not affect the original value of variable in calling function.
call by reference:-
#include <stdio.h>
int foo(int *x)
{
return *x;
}
int main() {
int a=1;
foo(&a);
printf("value of a is: %d",a);
return(0);
}
when we use the call by reference then:-
- While passing parameter using call by address scheme , we wiil be passing the actual address of the variable to the called function.
- Any updates made inside the called function will modify the original copy since we are directly modifying the content of the exact memory location.
in both the situations output will be :-