PPA-3
Question
Write a function maxval
that accepts three integers a
, b
and c
as arguments. It should return the maximum among the three numbers.
You do not have to accept input from the user or print output to the console. You just have to write the function definition.
Hint
This function accepts three arguments and returns a single number as output. Functions can have multiple arguments and this function is an example of that. Here is a code snippet to help you solve the problem:
This is obviously an incomplete solution and may even be incorrect. How will you extend this to solve the entire problem? How many return statements would you have to use if you proceed along these lines? Do you require these many return statements? Can you come up with a solution that has just one return statement?
Solution
Note that it is important to check for equality. Study what happens if all \(\geq\) are changed to \(>\).
How is this different from solution-1?
Make \(a\) the largest of the three numbers by swapping it with \(b\) and \(c\) if required. What would happen if we change the second if
into an elif
? We don’t seem to be checking for equality here. Is that fine?