2,835 views
2 votes
2 votes

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. 

2 Answers

Best answer
5 votes
5 votes

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
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 .

Answer:

Related questions