6,976 views

In which of the following cases is it possible to obtain different results for call-by-reference and call-by-name parameter passing methods?

1. Passing a constant value as a parameter
2. Passing the address of an array as a parameter
3. Passing an array element as a parameter
4. Passing an array

### 1 comment

What if it was call by ref vs call by value?

Correct Option: C

Passing an array element as a parameter is the answer.

Consider this function call

{
....
a[] = {0,1,2,3,4};
i = 0;
fun(a[i]);
print a[0];
}

fun(int x)
{
int i = 1;
x = 8;
}

Output:

• call-by-reference$: 8$
• call-by-name$: 0$

In Call-by-name, each occurrence of the formal parameter is replaced by the actual argument text. So, the function fun will be executed like:

{
int i = 1;
a[i] = 8; //a[1] is changed to 8 and not a[0]
}
by

Sir, I have a doubt please see-
I am  not sure please verify if i am thinking right or not.

Here, in this example a[1] will get changed only if i is global variable.

main()
{
....
a[] = {0,1,2,3,4};
i = 0;
fun(a[i]);
print a[0];
}

fun(int x)
{
int i = 1;
x = 8; // The argument expression (a[i]) is re-evaluated, and i refers to the value of i which is in main(), not the i which is here.
}

In case if code have been like this :-

int i = o;
main()
{
....
a[] = {0,1,2,3,4};
fun(a[i]);
print a[0];
}

fun(int x)
{
i = 1;
x = 8;  // The argument expression (a[i]) is re-evaluated, and i refers to the global value which is changed now, so a[1] will get changed
}


In 2nd code only a[1] should be changed not in first one.

@ arjun sir, same thing happens for expressions also, they give different results because in call by name expression will be reevaluated in the function body?? Am i right??
Shouldn't i be used as global variable.

yes it should be global.

 begin
integer n;
procedure p(k: integer);
begin
print(k);
n := n+10;
print(k);
n := n+5;
print(k);
end;
n := 0;
p(n+1);
end;


Output:

call by value:     1  1  1
call by name:      1 11 16
edited
yes

@, in question it is saying for call by name means formal parameter will replace by actual parameter so in Fun2 in your example when you'll pass 'B' then array P will be replaced by B so in that case both will generate the same output.

https://stackoverflow.com/questions/838079/what-is-pass-by-name-and-how-does-it-work-exactly

a parameter such as a[i] depends on the current value of i inside the function, rather than referring to the value at a[i] before the function was called

begin
integer n;
procedure p(k: integer);
begin
print(k);
n := n+10;
print(k);
n := n+5;
print(k);
end;
n := 0;
p(n+1);
end;


Output:

call by name:     1  11  16
Call be reference:  1  1  1   // is output correct for reference

@Arjun

@ Sachin sir , the given question has out of syllabus tag . IS it really out of syllabus ? becoz it is from the topic parameter passing in C and C is in syllabus

As the variable in the called function and the variable in the caller function is clashing.

shouldn’t the renaming of local variable in called function will be done ?

Local i in the called function will have no affect and A[0] = 8 will happen.

(a) passing a constnt value as parameter

(if any mistake in my ans plz correct it)
by

### 1 comment

Passing a constant value will produce the same result in both these parameter passing mechanisms or in almost any of the parameter passing mecahinsms.