Challenge: Max with Nested Functions
Explore how to define nested functions in Scala by writing a max function inside mainMax that returns the maximum of three integers. This lesson helps you practice parameter handling, return values, and functional nesting to enhance your Scala programming skills.
We'll cover the following...
Problem Statement
In this challenge, you need to create a nested function max which will be defined in the body of the function mainMax. mainMax returns the maximum of three numbers using the max function.
You will need to write the max function from scratch. Take some time and try to figure out how many parameters it should have and what the return value will be. You will also need to figure out what mainMax will return. Remember how in the previous lesson sqrt was returning the return value of sqrtIter.
Input
The inputs of the mainMax function are three numbers a, b, and c of type Int.
Output
The output will be the maximum of a, b, and c.
Sample Input
(1,9,5)
Sample Output
9
Test Yourself
Write your code in the given area. Try the exercise by yourself first, but if you get stuck, the solution has been provided. Good luck!
Let’s go over the solution review in the next lesson.