Programming-for-Output-Problems
October 6, 2023Programming-for-Output-Problems
October 6, 2023Programming-for-Output-Problems
Question 29 |
Consider the following code fragment
void foo(int x, int y)
{
x+=y;
y+=x;
}
main()
{
int x=5.5;
foo(x,x);
}
What is the final value of x in both call by value and call by reference, respectively?
void foo(int x, int y)
{
x+=y;
y+=x;
}
main()
{
int x=5.5;
foo(x,x);
}
What is the final value of x in both call by value and call by reference, respectively?
5 and 16 | |
5 and 12 | |
5 and 20 | |
12 and 20 |
Question 29 Explanation:
Call by Value:
The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
foo(x,x) —->foo(5,5)
Modification of x and y values won’t effect in the main program, So still x will hold value “5” because the data type is integer.
Call by Reference:
The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.
Function call foo(x,x)—foo(5,5) calls the function
void foo(int x, int y)
{
x+=y;
y+=x;
}
Here x and y variables, both pointing to same variable “x” in the main function.
So, if any modification done in the function it automatically reflect in the main function.
x+=y means x=x+y=5+5=10 // The variable “x” of main function will hold value 10 which used by both local variables of x and y
y+=x means u=y+x= 10+10=20 // Y value is 10 because , it point to variable ”x” in the main function.
The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
foo(x,x) —->foo(5,5)
Modification of x and y values won’t effect in the main program, So still x will hold value “5” because the data type is integer.
Call by Reference:
The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.
Function call foo(x,x)—foo(5,5) calls the function
void foo(int x, int y)
{
x+=y;
y+=x;
}
Here x and y variables, both pointing to same variable “x” in the main function.
So, if any modification done in the function it automatically reflect in the main function.
x+=y means x=x+y=5+5=10 // The variable “x” of main function will hold value 10 which used by both local variables of x and y
y+=x means u=y+x= 10+10=20 // Y value is 10 because , it point to variable ”x” in the main function.
Correct Answer: C
Question 29 Explanation:
Call by Value:
The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
foo(x,x) —->foo(5,5)
Modification of x and y values won’t effect in the main program, So still x will hold value “5” because the data type is integer.
Call by Reference:
The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.
Function call foo(x,x)—foo(5,5) calls the function
void foo(int x, int y)
{
x+=y;
y+=x;
}
Here x and y variables, both pointing to same variable “x” in the main function.
So, if any modification done in the function it automatically reflect in the main function.
x+=y means x=x+y=5+5=10 // The variable “x” of main function will hold value 10 which used by both local variables of x and y
y+=x means u=y+x= 10+10=20 // Y value is 10 because , it point to variable ”x” in the main function.
The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
foo(x,x) —->foo(5,5)
Modification of x and y values won’t effect in the main program, So still x will hold value “5” because the data type is integer.
Call by Reference:
The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.
Function call foo(x,x)—foo(5,5) calls the function
void foo(int x, int y)
{
x+=y;
y+=x;
}
Here x and y variables, both pointing to same variable “x” in the main function.
So, if any modification done in the function it automatically reflect in the main function.
x+=y means x=x+y=5+5=10 // The variable “x” of main function will hold value 10 which used by both local variables of x and y
y+=x means u=y+x= 10+10=20 // Y value is 10 because , it point to variable ”x” in the main function.
Subscribe
Login
0 Comments