180k views
4 votes
Consider the following program written in C-like syntax: void incr(int first, int second) { first = first + 1; second = second + 1; } void main() { int a=3; b=5; incr(a,b) ; } For each of the following parameter-passing methods, what are the values of the vari- ables a and b after execution? Explain in detail. (a) call by value (b) call by reference (c) call by value-result

User Nevsan
by
5.8k points

1 Answer

2 votes

Answer:

Check the explanation

Step-by-step explanation:

By doing call by value parameter passing the values remain unchanged even after the function call as the values of actual parameters cannot be altered with the help of the formal parameters. Here is an example code of the above stated C syntax and it’s output clearly shows that there is no change in the values even after function incr is called.

Code in C:-

#include <stdio.h>

// function incrementing the values

void incr(int first, int second)

{ printf("\\Before updation in the values\\first = %d second = %d",first,second);

// incrementing the first and second values

first = first + 1;

second = second+ 1;

printf("\\After updation in the values\\first = %d second = %d",first,second);

}

int main()

{ // Decalring the values of a and b

int a=3, b=5;

printf("\tCALL BY VALUE\\\\");

printf("Before the function call\\a = %d b = %d",a,b);

// Value passing in the function incr

incr(a,b);

printf("\\After the function call\\a = %d b = %d",a,b);

return 0;

}

Kindly check the first and second attached image below for the code screenshot and code output.

By doing call by reference parameter passing the values change after the function call as the values of actual parameters can be altered with the help of the formal parameters. Values are passed as a reference in this case. Here is an example code of the above stated C syntax and it’s output clearly shows that there is change in the values after function incr is called.

Code in C:-

#include <stdio.h>

// function incrementing the values

void incr(int *first, int *second)

{ printf("\\Before updation in the values\\first = %d second = %d",*first,*second);

// incrementing the first and second values

(*first)=(*first)+ 1;

(*second)=(*second)+ 1;

printf("\\After updation in the values\\first = %d second = %d",*first,*second);

}

int main()

{ // Decalring the values of a and b

int a=3, b=5;

printf("\tCALL BY REFERENCE\\\\");

printf("Before the function call\\a = %d b = %d",a,b);

// Reference passing in the function incr

incr(&a,&b);

printf("\\After the function call\\a = %d b = %d",a,b);

return 0;

}

Kindly check the third and fourth attached image below for the code screenshot and code output.

Consider the following program written in C-like syntax: void incr(int first, int-example-1
Consider the following program written in C-like syntax: void incr(int first, int-example-2
Consider the following program written in C-like syntax: void incr(int first, int-example-3
Consider the following program written in C-like syntax: void incr(int first, int-example-4
User Mithlesh Kumar
by
5.4k points