• edited by
8,232 views

1 Answer

Best answer
26 26 votes
  • Call by Value : 3
  • Call by Reference : 12
  • Call by Copy-Restore : 12

The $3$ parameter passing mechanisms are simulated in the following 'C' codes. 

PS: C language only supports Call by Value and even in the case of pointers, the value of the pointer is getting passed explicitly in a pointer variable. This is different from call by reference (say in C++) where this happens implicitly. The following code for Call-by-Reference and Call-by-copy-restore is just a simulation of the parameter passing behaviour and their implementation in any language need not be the same.

  1. Call by Value
    #include <stdio.h> 
    int foo(int x,int y,int z)
    { 
        y = y+1; 
        z = x+x; 
    } 
    int main(void)
    { 
        int a = 3; 
        int b = 3; 
        foo(a+b,a,a); 
        printf("%d",a); 
        return 0; 
    }  
    
  2. Call by Reference (Call by reference is simulated by passing address in C) :
    #include <stdio.h>
    int foo(int *x,int *y,int *z)
    { 
        *y = *y+1; 
        *z = *x+*x;
    } 
    int main(void) 
    { 
        int a = 3; 
        int b = 3; 
        int c = a+b; 
        foo(&c,&a,&a); 
        printf("%d",a); 
        return 0; 
    }   
    
  3. Call by Copy-Restore:
    #include <stdio.h> 
    void foo(int *x,int *y,int *z)
    { 
        *y = *y+1; 
        *z = *x+*x;
    } 
    int main(void)
    { 
        int a=3; 
        int b=3; 
        int c=a+b; 
        int d,e;
        d = c;//copy 
        e = a;//copy 
        foo(&d,&e,&e); 
        a = e;//restore 
        c = d;//restore 
        printf("%d",a); 
        return 0;
    }
• selected by
1 flag:
✌ Edit necessary (P0535_Yedidyah_Sagar “Call by copy-restore, gives unspecified behavior for this code.http://pages.cs.wisc.edu/~fischer/cs536.s08/course.hold/html/NOTES/9.PARAMETER-PASSING.html”)
Position:
Show:

Related questions

43 43 votes
6 answers 6 answers
17.1k
17.1k views
Kathleen asked Sep 23, 2014
17,054 views
The number of binary strings of $n$ zeros and $k$ ones in which no two ones are adjacent is$^{n-1}C_k$$^nC_k$$^nC_{k+1}$None of the above
27 27 votes
3 answers 3 answers
7.1k
7.1k views
go_editor asked Apr 24, 2016
7,082 views
For the following code, indicate the output if static scope rulesdynamic scope rulesare usedvar a,b : integer; procedure P; a := 5; b := 10; end {P}; procedure Q; var a, ...
13 13 votes
3 3 answers
5.0k
5.0k views
Kathleen asked Sep 29, 2014
4,962 views
A stack is used to pass parameters to procedures in a procedure call.If a procedure $P$ has two parameters as described in procedure definition:procedure P (var x :intege...
31 31 votes
3 answers 3 answers
5.5k
5.5k views
Kathleen asked Sep 12, 2014
5,494 views
Consider the following pseudo-code (all data items are of type integer): procedure P(a, b, c); a := 2; c := a + b; end {P} begin x := 1; y := 5; z := 100; P(x, x*y, z); W...