CS 1073 Introductory Programming
for Scientific Applications
Functions in Java


Functions in algebra -- volume of a sphere: Think about finding the volume V of a Sphere of radius r when r is 3. In ordinary algebra (not Java), we would have equations:

Here the 3 is called the actual parameter (it's the number we actually plug into the formula for volume). Notice that the equals signs above are not assignments, but are mathematical equals -- quite different and yet similar in some ways.

The r in the formula is called the formal parameter (it's the variable that is given the actual parameter's value). It's formal because it doesn't matter what name we use for it, so that we could write:

These are all the same function V and any one would work as well as another. (Plugging 3 in for t in the second formula above would give the same result as for the formula involving r.)


Functions in Java -- Volume of a sphere: In Java it's very similar, except that there are some extra words and curley brackets, etc. Follow the pattern below. Put the extra function V just above the function main.

Everything except the red above are just little Java "love notes" or "magic parts" that have to be that way for it to work, but these additions are mostly the same for each application. Here is the output when this program is run:

In the function main, the presence of V(3) is a call to the function V with actual parameter 3. One says that the actual parameter 3 is passed to the function V. It is just as if there were an assignment statement r = 3; at the start of the function V. Then the expression (4.0/3.0)*Math.PI*r*r*r is evaluated (plugging in 3 for r) and the resulting double is returned to the point of call, the V(3). It is as if the value 113.09... just "pops in" to replace the V(3).

The first line of the function V looks like:

There are two occurrences of double here, both in red. The second one says that the formal parameter r is supposed to be a double, that is, a high-precision real number in the computer. Even though the call was V(3) with an int for the actual parameter, this 3 is converted to the double equal to 3.0 as it is passed to the function V. The first double in red says that the function should return a double, rather than an int, say. The words public static at the beginning we'll talk about later.


Example of abs, another function with one parameter: Here we are writing our own function abs for doubles, although you could just use Math.abs instead.

Here are results of a run.


A function with two parameters: min: Java provides the function Math.min:

When run, this produces:

But we can write our own function min, with two int parameters that returns an int:

This produces the same output 13 that we had before, but when we try the function call min(13.8, 47.2), we get an error. This is because the min function that we wrote is expecting two int parameters, and we are giving two doubles. We can give a separate definition of min for the other parameters as below:

When run, this produces:

The first call to min uses the first definition (with two int parameters), while the second call uses the second definition (with two double parameters). Normally the third call wouldn't be able to use either definition, but this situation is a special case, since in the third call, the int parameter 13 is first converted to a double for use by the second min definition. In fact, in this special case the second definition will work for all three calls because ints can be converted to doubles. As an exercise, put an extra printing statement inside each of the two min functions above to see which one is being called.


A function with no parameters that returns a value: We have already used a function with no parameters when we wrote: Math.random() to get a random double between 0.0 and 1.0. We can write our own functions like this. For example, suppose we wanted a function pi that would always return the value of pi, or 3.141592653589793.

Of course,this just prints the value 3.141592653589793. Notice that both the call pi() and the definition use empty parentheses () for the parameters. These parentheses are required, even though there are no parameters. (They help us keep straight that this is a function with no parameters and not just an identifier.).

In real programs, we would want to use the constant Math.PI instead of this function, or else we could define our own constant using:

In both of these cases you must not put in parentheses after the PI


A function with no parameters that returns nothing: We can also have a function with no parameters that returns nothing (void) as the following example shows:

Here the output is just Hello!!. The void in the function definition is what says that nothing is returned. Because it returns nothing, you cannot have a return inside the funtion. When you call the function, it cannot be inside a printing statement or in an expression, but must be by itself.


Sum of squares: Start with the formula for the sum of the squares of positive integers up to and including N:

For example, if N = 4, then the sum is: 12 + 22 + 32 + 42 = 1 + 4 + 9 + 16 = 30. The formula gives: 43/3 + 42/2 + 4/6 = 64/3 + 16/2 + 4/6 = (21 + 1/3) + 8 + 2/3 = 30.

This function might seem a bit strange because it always yields an exact integer, even though the individual terms may not be integers. If N is divisible by 6, say N = 12, then each part of the formula is an integer:

Otherwise however, the individual terms won't be integers, as for example when N = 10:

Here is a program that calculates the sum and compares it with the result of the formula:

Below I have rewritten the above simple formula to use a number of functions. This illustrates functions square, cube, sumOfSquares, and sumOfSquaresFormula, as well as main. This example artificially uses more functions than necessary. Here is the program itself:

Here are results of runs with N = 10 (as above), and with N = 40, N = 100, N = 500.