Consider the following C program: #include<stdio.h> int jumble(int x, int y){…
2019
Consider the following C program:
#include<stdio.h>
int jumble(int x, int y){
x = 2*x+y;
return x;
}
int main(){
int x=2, y=5;
y=jumble(y,x);
x=jumble(y,x);
printf("%d \n",x);
return 0;
}
The value printed by the program is ________.
Attempted by 196 students.
Show answer & explanation
Correct answer: 26
Answer: 26
Initial values: x = 2, y = 5.
First call: y = jumble(y, x). The function receives the first argument as the local parameter x = 5 and the second as local parameter y = 2. Inside the function it computes x = 2*x + y = 2*5 + 2 = 12 and returns 12, so y becomes 12.
Second call: x = jumble(y, x). Now y = 12 and x = 2, so the function receives local x = 12 and local y = 2. It computes x = 2*12 + 2 = 26 and returns 26, so x becomes 26.
Finally, printf prints 26.
Note: The function uses pass-by-value, so only the returned value assigned back to the caller changes the caller's variables.
A video solution is available for this question — log in and enroll to watch it.