The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+2 votes
1.2k views

What will be the output of the following program assuming that parameter passing is

  1. call by value

  2. call by reference

  3. call by copy restore

procedure P{x, y, z}; 
begin 
y:y+1; 
z: x+x 
end; 
begin 
a:= b:= 3; 
P(a+b, a, a); 
Print(a) 
end.   
asked in Programming by Veteran (59.9k points)
edited by | 1.2k views

1 Answer

+6 votes
  Call by Value : 3
  Call by Reference : 12
  Call by Copy-Restore : 12

Equivalent 'C' Code for :

  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; 
}
#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; 
} 
 #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;
 }
answered by Active (3k points)
edited by
+2
Aditya's call by reference program :

 //Call by Reference(Call by reference is realised 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;
    
    
}  

 

Call by copy-restore program :

//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;
    
    
}

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
47,932 questions
52,335 answers
182,384 comments
67,817 users