Problem overview
Given three integers a, b and c, return the largest. The lesson splits the work between two functions. The helper maxOfTwo takes two numbers and returns the larger one. The graded function maxOfThree must find the largest of three by calling the helper, without comparing numbers itself.
For a = 3, b = 7, c = 5 the answer is 7. For a = -3, b = -8, c = -1 the answer is -1, the negative number closest to zero. For a = 4, b = 4, c = 2 the answer is 4: when two values tie, either one is the answer because they are equal.
Approach: solve for two, then reuse it
Finding the larger of two numbers is a small problem with a small solution. Finding the largest of three reduces to it: the largest of three is the larger of "the larger of the first two" and the third number. If the helper already answers the two-number question, maxOfThree only has to call it twice.
This is the main reason to write helper functions: give a small job a name, get it right once, and build on it. The same helper would extend to four or five numbers by nesting more calls.
Step by step
- In
maxOfTwo, returnawhena > b. Areturnends the function immediately, so nothing after it runs for that case. - Return
bbelow theif. The function only reaches this line whenais not greater thanb. Then eitherbis larger or the two are equal, andbis correct in both cases. Noelseis needed. - In
maxOfThree, return one call inside another:maxOfTwo(maxOfTwo(a, b), c). The inner call runs first and gives the larger ofaandb. That result becomes the first argument of the outer call, which compares it withc.
The solution in Go
func maxOfTwo(a int, b int) int {
if a > b {
return a
}
return b
}
func maxOfThree(a int, b int, c int) int {
return maxOfTwo(maxOfTwo(a, b), c)
}
A few Go details:
- The return type comes after the parameters. In
func maxOfTwo(a int, b int) int, the lastintis the type of the value the function returns. - Each function has its own variables. Both functions have parameters named
aandb. They are separate: whenmaxOfThreecallsmaxOfTwo(a, b), the helper receives copies of the values. - A function that returns a value must return on every path. If
maxOfTwoended after theifwithout the finalreturn b, the compiler would report a missing return. - Call order. Go evaluates the arguments of a call before the call itself, so the inner
maxOfTwo(a, b)is finished before the outer one starts.
Edge cases and common mistakes
- Negative numbers. Starting a "largest so far" at 0 would be wrong for
-3, -8, -1, which would give 0. The helper only compares the inputs, so it is correct for any sign. - Ties. With
a = 4andb = 4,a > bis false, so the helper returnsb, which is 4. Using>=would also work. - Position of the maximum. The largest value can be the first, the middle or the last argument. For
(9, 1, 2),(3, 7, 5)and(1, 2, 9)the results are 9, 7 and 9. - Comparing in the wrong function. The lesson wants
maxOfThreeto rely on the helper alone, with noifof its own.
Testing
a = 3, b = 7, c = 5gives7.a = -3, b = -8, c = -1gives-1.a = 4, b = 4, c = 2gives4.a = 9, b = 1, c = 2gives9.a = 1, b = 2, c = 9gives9.
