search
Log In
2 votes
915 views

Consider the following statements $S1, S2$ and $S3$:

$S1$: In call-by-value, anything that is passed into a function call is unchanged in the caller’s scope when the function returns.

$S2$: In call-by-reference, a function receives implicit reference to a variable used as argument.

$S3$: In call-by-reference, caller is unable to see the modified variable used as argument. 

  1. $S3$ and $S2$ are true. 
  2. $S3$ and $S1$ are true.
  3. $S2$ and $S1$ are true. 
  4. $S1, S2, S3$ are true. 
in Compiler Design 915 views

3 Answers

5 votes
 
Best answer

Answer : C

Call-By-Value : While Passing Parameters using call by value, copy of original parameter is created and passed to the called function.
Any update made inside method will not affect the original value of variable in calling function. For Example 

#include<stdio.h>

void interchange(int number1,int number2)
{
    int temp;
    temp = number1;
    number1 = number2;
    number2 = temp;
}

int main() {

    int num1=50,num2=70;
    interchange(num1,num2);

    printf("\nNumber 1 : %d",num1);
    printf("\nNumber 2 : %d",num2);

    return(0);
}

In the above example num1 and num2 are the original values and copy of these values is passed to the function and these values are copied into number1,number2 variable of interchange function respectively.
As their scope is limited to only function they cannot alter the values inside the caller function.

Call-By-Reference : This method copies the address of an argument into the formal parameter. This means that changes made to the parameter affect the original argument.

#include<stdio.h>

void interchange(int &num1,int &num2)
{
    int temp;
    temp  = num1;
    num1 = num2;
    num2 = temp;
}

int main() {

    int num1=50,num2=70;
    interchange(&num1,&num2);

    printf("\nNumber 1 : %d",num1);
    printf("\nNumber 2 : %d",num2);

    return(0);
}

            

C uses call by value to pass arguments while C++ has both call by value and call by reference. C can simulate call by reference by passing pointers as value- but this is also technically call by value, as pointer value is copied and passed.        


selected by
4 votes

Answer is C) S2 and S1 are true

S3 is false, because in call-by-reference caller always sees the modified value of the argument .

0 votes
Answer C
Answer:

Related questions

17 votes
3 answers
1
3.8k views
What is printed by the print statements in the program $P1$ assuming call by reference parameter passing? Program P1() { x = 10; y = 3; func1(y,x,x); print x; print y; } func1(x,y,z) { y = y + 4; z = x + y + z } $\text{10, 3}$ $\text{31, 3}$ $\text{27, 7}$ None of the above
asked Sep 15, 2014 in Programming Kathleen 3.8k views
2 votes
1 answer
2
452 views
Consider the procedure declaration: Procedure P (k: integer) where the parameter passing mechanism is call-by-value-result. Is it correct if the call, P (A[i]), where A is an array and i an integer, is implemented as below. create a new local variable, say z; assign to ... the body of P using z for k; set A [i] to z; Explain your answer. If this is incorrect implementation, suggest a correct one.
asked Dec 19, 2016 in Compiler Design jothee 452 views
16 votes
1 answer
3
828 views
What is printed by following program, assuming call-by reference method of passing parameters for all variables in the parameter list of procedure P? program Main(inout, output); var a, b:integer; procedure P(x, y, z:integer); begin y:=y+1 z:=x+x end P; begin a:=2; b:=3; p(a+b, a, a); Write(a) end.
asked Dec 19, 2016 in Compiler Design jothee 828 views
0 votes
2 answers
4
464 views
What does the following program output? program module (input, output); var a:array [1...5] of integer; i, j: integer; procedure unknown (var b: integer, var c: integer); var i:integer; begin for i := 1 to 5 do a[i] := i; b:= 0; c := 0 for i := 1 to 5 do write (a[i]); writeln(); a[3]:=11; ... (c,b); b := 5; c := 6; end; begin i:=1; j:=3; unknown (a[i], a[j]); for i:=1 to 5 do write (a[i]); end;
asked Nov 25, 2016 in Compiler Design makhdoom ghaya 464 views
...