Yes.
In call by value, values are computed by the caller and copied into the formal parameters of the called method. It is fine if computing a value involves another method.
public class UnitTestMax { public static int maxTwo( int X, int Y) { if ( X > Y ) return X; else return Y; } public static int maxFour( int A, int B, int C, int D) { int maxAB = maxTwo( A, B ); int maxCD = maxTwo( C, D ); return maxTwo( maxAB, maxCD ); } public static int maxEight( int A, int B, int C, int D, int E, int F, int G, int H) { int maxABCD = maxFour( A, B, C, D ); int maxEFGH = maxFour( E, F, G, H ); return maxTwo( maxABCD, maxEFGH ); } public static void main (String[] args ) { int A= 1, B= 3, C= 4, D= 30; int E= 45, F= 7, G= 21, H= 7; System.out.println("The max of " + A + ", " + B + ", " + C + ", " + D + ", " + E + ", " + F + ", " + G + ", " + H + " is: " + maxEight( A, B, C, D, E, F, G, H) ); } }
To compute the maximum of eight integers,
use maxFour()
twice
(which uses maxTwo()
twice.)
Scoping rules keep everything in order.
You don't need to think of new parameter names for each method.
maxTwo()
gets called seven times in the above, each time
with different values bound (assigned) to its formal parameters.
The picture shows this happening.
Each circle shows a particular call of the named method
with values copied into the method
(for example, 1, 3, 4, 30)
and the maximum of those values returned to the caller
(for example, 30).
The red numbers show the order of the calls.
The first call is to maxEight()
who ultimately returns 45 to its caller.
(Though Question: ) How would you compute the maximum of seven integers?