Function Results as Arguments

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

    
    // calculate its cosine
    double result = Math.cos( Math.toRadians(degrees) );

Function Results as Arguments

You may have noticed that one function is nested inside another:

double result = Math.cos( Math.toRadians(degrees) );

This is perfectly fine, and is a reasonable way to write the code. As long as the inner function returns a value that is correct for the outer function this works fine. In fact, we could combine this line with the println():

// calculate its cosine and print the result
System.out.println("cosine: " + Math.cos(  Math.toRadians(degrees) ) );

It is a matter of taste, sometimes, how much to combine computations into one statement. Your goal should be to make the logic clear, not to save typing.

QUESTION 22:

Which page in this chapter is your favorite?