retagged by
4,484 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
retagged by

1 Answer

Best answer
23 votes
23 votes

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
Answer:

Related questions

3 votes
3 votes
1 answer
1
makhdoom ghaya asked Dec 5, 2016
1,270 views
Will recursion work correctly in a language with static allocation of all variables? Explain.
5 votes
5 votes
1 answer
2