Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Parameter Passing Example from a Previous Exanm void main() int aM[] = {10, 20,

ID: 3588599 • Letter: P

Question

Parameter Passing Example from a Previous Exanm void main() int aM[] = {10, 20, 30} int k = 2; sub(aM[k], k); print ('main', k, aM[0], aM[1], aM[2]); void sub(int x, int y) x += 3; printf('sub', x, y); Trace and show the output produced by the code using the following parameter transmission schemes: a. by value b. by true reference c. by copy-restore reference d. by name Problem a. by value aM 10 20 30 Problem b. by true ref aM 10 20 30 main k 2 k 2 sub Output sub sub main main Problem c. by copy-restore aM 10 20 30 Problem d. by name aM 10 20 30 main k 2 k 2 sub Output sub sub main main

Explanation / Answer

Pass by value means the called functions' parameter will be a copy of the callers' passed argument.That simply means whatever changes are made on the copy will not be reflected back on the true values. so here in our question in part a. we are passing two values by pass by value.

here k=2, so am[2]=30 because starting index of any array is zero.

So x=30, y=2 as the values are passed in the main function.

y-=1;// 1

x+=3;//33

k-=1;// 1 because the scope of k is in the entire main fucntion and sub is inside main

sub 33 1

and the output of main will be

main 2 10 20 30

part b

for call by referrence

any changes done on the values passed by referrence will be reflected back so output will be

sub 33 1

main 1 10 20 33

part c.

The basic difference between call by reference and copy/restore then is that changes made to the function variable will not show up in the passed in variable until after the end of the function while call by reference changes will be seen immediately.

sub 33 1

main 1 10 20 33

part d.

For actual parameters that are simple variables, this is the same as call by reference. For actual parameters that are expressions, the expression is re-evaluated on each access. It should be a runtime error to assign into a formalparameter passed by name, if the actual parameter is an expression.

sub 33 1

main 1 10 20 33

feel free to ask any doubt in comments section