in Compiler Design retagged by
4,540 views
21 votes
21 votes

In which of the following case(s) is it possible to obtain different results for call-by-reference and call-by-name parameter passing?

  1. Passing an expression as a parameter
  2. Passing an array as a parameter
  3. Passing a pointer as a parameter
  4. Passing as array element as a parameter
in Compiler Design retagged by
4.5k views

2 Comments

2
2

ans will be D

0
0

1 Answer

23 votes
23 votes
Best answer

Answer A, D.

A is correct as call-by-name works like a macro and substitution happens only during use time. For example if we pass $2+3$ to the below function

int foo(int x)
{
    return x * x;
}

we get $2+3*2+3$ which will be $11$ due to the higher precedence for $*.$ But, call by reference will return $5*5 = 25.$ (For call by reference, when an expression is passed, a temporary variable is created and passed to the function) 

D is also correct: Passing an array element as a parameter

See the below example:

void m(int x,int y){
    for(int k = 0;k < 10;k++){
        y = 0; x++;
    }
}

int main(){
    int j; int A[10];
    j = 0;
    m(j,A[j]);
    return 0;
}


For the above example if we use 'Call by name' its initialize all the array elements with $0.$ But if we apply ' Call by Reference ' it will only initialize $A[0]$ with $0.$

selected by

10 Comments

How to pass 2+3 as actual parameter? Pls explain using calling statement.

@mystylecse

0
0

Interestingly the pass by value seems to be giving output as 25 and not 10.

Check ideone link.

#include <stdio.h>

int foo(int x)
{
    return x * x;
}

int main(void) {
	// your code goes here
	int a = foo(2+3);
	printf("a is %d\n",a);
	return 0;
}

 

1
1
It's 25  because C does not support call by name.
4
4

@Ekta07_GATE

You are right!

//Simulating pass by name in C using macros
#include <stdio.h> 
#define MULTIPLY(a, b) a*b 
int main() 
{ 
    // The macro is expanded as 2 + 3 * 3 + 5, not as 5*8 
    printf("%d", MULTIPLY(2+3, 3+5)); 
    return 0; 
}

Ideone link.

Source: geeksforgeeks example 4

 

1
1

@Arjun sir

i am getting a doubt with option b ie. Passing an array as parameter. Consider code below:

Let integer pointer takes 4 bytes and integer also takes 4 bytes.

int fun(int a[])

{

int n= sizeof(a)/sizeof(a[0]);

printf("%d",n);

}

int main()

{

int arr[10];

fun(arr);

return 0;

}

Call by name: 10

Call by reference: 1

Sir please clarify.

0
0
Please anyone clarify my doubt
0
0

As, C follows strict evaluation won’t the $f(2+3)$ will be same as $f(5)$ here, after evaluating internally before passing.

@Abhrajyoti00

0
0

@Pranavpurkar Yes you are correct in terms of C-language. 

But this q does not say C-language. This is because call by name is not supported by C.

The ans will be Option A and D w.r.t this q.

See also: Programming: GATE CSE 1994 | Question: 1.20 (gateoverflow.in)

1
1
okay,Thanks bro!
1
1

What’s the proof that there doesn’t exist any situation where at least one of the option (B) or (C) is true?

0
0
Answer:

Related questions