Functions

This will be a discussion of functions in Java. There will also be notes on the use of reference parameters.

Functions

All programming languages have functions. They can be called different things. Subroutine, routines, procedures and functions all describe the same thing. A function is a piece of code that is run by itself. Control is transferred to it from the main program and control returns after the function is completed.

Digression

The control flow of a program is the sequence of instructions that it executes when it runs. The default order in Java is to start at the first statement in the main function and continue from the top to the bottom. We have already seen some control statements that can alter the control flow.

Functions can take arguments and can return results. The general form of a function in Java is

return_type
func_name(type1 arg1, type2 arg2,...) {
     // code for the function
} // func_name

A function can have no arguments. In Java, a function is called by using its name followed by a parenthesized list of arguments.

// Convert Fahrenheit to Celsius
float
ftoc(float ftemp) {
   float ctemp;
   ctemp = (((ftemp -32 ) *5) / 9.0 );
   return(ctemp);
} // ftemp The return type is float. This means that anywhere a float can be used, a call to this function can be used. Its name is ftoc and it takes one argument, a floating point number. At the end, it returns a float value. In between the braces is the code that implements the function. Variables can be declared inside the function that are local to the function. This means that the variables, like len, are only used inside the function. This variable len would be different from another variable len used somewhere else in the program. A few other comments. If the function doesn't return anything, the return type is void. It is possible to make a function that takes an unknown number of arguments, but this works in a very different way.

Some details about the return statement. If the function doesn't return a value, the return statement is optional as a return occurs when the end of the function is reached. But a return statement can be used if you want to exit the function early. In this case, the form is just return; If there is a value to return, there are two forms. Both forms work exactly the same.
return expr; return (expr);
A function can return only one value.
About function calls and how they work.
A typical function call looks like this
new_temp=ftoc(32.0);
When this is executed, the control transfers

from where the call to ftoc is in the code to the beginning of the code in the ftoc body. The variables described in the function header are set with the values in the function call. The variables in the function header are called the formal parameters. The values in the call to the function are called the actual parameters. The formal parameters are like local variables in the function code. The code in the function is executed in the usual order. When a return statement is executed, control goes back to the main program just at the point it left. If the function returns a value, then it is as if that value was put in place of the function call. So if the actual call was like this

temp = ftoc(32.0; // note the use of an float constant
Then control starts at the top of the ftoc function. The formal parameter is a float. All the arguments to the function are evaluated before the code is executed. But Java doesn't guarantee that they are evaluated in any particular order. So don't assume it is left to right. Then the formal parameter, ftemp is set to the value 32.0. The arithmetic is done using the value in ftemp. The result is stored in the local variable ctemp. In this case, the value is 0.0 The return statement send the 0.0 back to the calling function. So the program acts as if the line above was changed to temp = 0.0; Then the program goes on from that point as if it had never called the function.

Parameter passing techniques

All parameters in a Java function are pass-by-value. There are two major ways information is passed to a function. One is pass-by-value. This means that the expression that is the parameter is evaluated and the result is stored in a local variable. This value is a copy of the original. In this example from the code,

rtc=ftoc(rtf);
A copy of the value of rtf is stored in the local variable ftemp. If we were to change the ftoc() function to add a line

ftemp=0.0;
this would have no affect on the value of rtf. This is because ftemp is a local variable and is not related to rtf.

Recursion is supported by Java. This allows a function to call itself. There are many problems, for example, linked lists, where recursion makes the program easier to understand. Key to the ability to do recursion is the fact that the formal parameters are local variables. So each time a function calls itself, there is a new copy of the variables. It is important to remember that the computer views all function calls the same, even if it is calling itself.

Reference Parameters

Java doesn't support pass by reference, at least not the way C++ does. When you pass an object into a method in Java, you are not making a copy of the object but passing a reference to the object. But it is still pass by value. So, if you change the parameter to refer to another object, this will have no effect on the original, only the copy in the local parameter. If you call methods on the parameter that change the state of the object, these changes are made on the original. This is because you are using the reference to access the methods on the object you passed in.