recategorized by
9,950 views
26 votes
26 votes

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
recategorized by

2 Answers

Best answer
48 votes
48 votes

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]
}


A very good read: http://courses.cs.washington.edu/courses/cse341/03wi/imperative/parameters.html

edited by
–1 votes
–1 votes
(a) passing a constnt value as parameter

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

Related questions

22 votes
22 votes
2 answers
1
Kathleen asked Oct 4, 2014
3,757 views
An unrestricted use of the "$goto$" statement is harmful becauseit makes it more difficult to verify programsit increases the running time of the programsit increases the...
37 votes
37 votes
3 answers
2
go_editor asked Sep 29, 2014
9,584 views
What does the following program print?#include<stdio.h void f(int *p, int *q) { p=q; *p=2; } int i=0, j=1; int main() { f(&i, &j); printf("%d %d\n", i,j); return 0; }$2 \...