What is the value returned by the method f() defined below public static int f(int x, int y) {return (x>y) ? y: x;}
Answers
The method takes two integer arguments and returns an integer. The return statement has an conditional operator which checks whether x is greater than y. If yes then the value of y is returned from the function. Otherwise the value of x is returned.
The return value depends purely on the value of x and y that is passed in the calling portion of the program. A conditional operator can be used to check a condition and write single true statement and single false statement.
Given code :
f(int x, int y)
{
return (x>y) ? y: x;
}
Return Value :
The function f(x,y) returns the SMALLEST VALUE between x and y.
Explanation :
- f(int x, int y) is the function, where x and y are the parameters of integer type passed during the function call.
- Once the function is called, it enters the function block and evaluates the composition or expression present in the retun statement.
- (x>y) ? y: x; is a conditional operating statement. Here, the compiler checks if x>y or not.
- If yes, it returns the value present in y (which will be the small value if condition becomes true)
- If the condition is false, it returns the value present in x (As condition is False, it means y has large value and x has the small one)
- So, the function returns the Smallest value among the 2 integer parameters sent. This happens IF AND ONLY IF, function call is made and that too with the parameters of INT type.
Example : C++ programming [See the Attachment]
#include <iostream>
using namespace std;
int f(int x, int y) //Function called; definition
{
return (x>y) ? y: x; // Conditional operation and returning small value
}
int main () //Program execution starts here
{
cout<<f(61,58); //Function call with integer type parameters
return 0;
}
Output :
58
Learn more :
1. Matrix addition program
https://brainly.in/question/17257855
2. what is output for snippet-s=0 for s in range(5): print(s)
https://brainly.in/question/8107617