## Want to keep learning?

This content is taken from the University of Reading's online course, Begin Programming: Build Your First Mobile Game. Join the course to learn more.
6.3

# Using functions

This step builds on the information provided in the video in the previous step.

Functions (also called ‘procedures’ in some programming languages and ‘methods’ in most object oriented programming languages) are a set of instructions bundled together to achieve a specific outcome. Functions are a good alternative to having repeating blocks of code in a program. Functions also increase the reusability of code. Values can be passed to a function using variables – we call these parameters or arguments. Functions can also return values.

Here is a simple function in Java.

int findMaximum(int num1, int num2) {
if (num1 > num2)
return num1;
else
return num2;
}


The function name is findMaximum. As can be interpreted from the name, the function is written to find the maximum of two given numbers. num1 and num2 are two integers passed into the function – these are the parameters. Notice that the function parameters are placed within parenthesis just after the function name. The int appearing before the function name denotes that the return type of this function is an integer. There is also a return statement in the function. This statement returns a value from the function. This value should be of the type defined as the return type of the function.

The function name and parameter types are called the ‘signature’ of the function. The signature of this function is findMaximum(int,int)

A more generalised definition for a simple function is:

return_datatype functionName(datatype parameter1, datatype parameter2) {
statements;
return a value of return datatype;
}


This function uses only two parameters but functions can use as many or as few parameters as required, or even none at all. For example, if you wanted a function to convert a temperature value from Fahrenheit to Celsius you would use a function with just one parameter because that is sufficient to do the work you want it to:

float convertToCelsius(float temperatureFahrenheit)


So, if you want to find the maximum of three values in a function this is what your code should look like:

int findMaxOfThree(int num1, int num2, int num3)

Note: when you learn how to call functions from your program you will be able to find the maximum of three integers using the findMaximum function we looked at before. You can call the findMaximum function two times to achieve the same – we will look at it later.

## Functions in the game

Now, look at some more examples from the game code:

public void cleanup()


This function was taken from GameView.java file that was in the bundle you downloaded to install the game program in your machine.

Can you find the return type, function name and parameters of this function?

The function name is ‘cleanup’ and its return type is void. This means that the function does not return anything – in some programming languages functions that do not return values are called procedures. It does not have any parameters.

### What does ‘public’ mean in the code?

‘public’ is an access specifier. These are used to control which parts of a program have access to certain content. We will not be covering these as part of the course, but if you are interested in learning more you can refer to the Java documentation, which provides a comprehensive Java tutorial on access control.

### Calling a function

How do we call a function? Here, again, is the example of findMaximum function:

Line 1: int number1 = 100;
Line 2: int number2 = 500;
Line 3: int myMax =  findMaximum (number1, number2);


Here in Line 3, we call the findMaximum function by passing the parameters 100 and 500. The value returned by the findMaximum function will be stored in the myMax variable. (Note: Line 1 and Line 2 are defining the variables and assigning values that are to be used in the function).

We could have also called this function as follows:

int myMax =  findMaximum (100, 500);


If the function does not return values you can simply call it without assigning the return value. For example, the cleanup function we looked at before can be called as:

cleanup ();


Now you know how to call a function in a program, can you work out how you would find the maximum of 3 integers (num1, num2 and num3) and assign it to an integer variable called myMax using the

int findMaximum(int num1, int num2)


Line 1:  int num1 = 100;
Line 2:  int num2 = 500;
Line 3:  int num3 = 600;
Line 4:  int temporary = findMaximum(num1, num2);
Line 5:  int myMax = findMaximum(temporary, num3);


Line 1 to Line 3 defines and assigns values to variables. In Line 4 the findMaximum function is called, with two parameters num1 and num2. The result of this function call is stored in the variable temporary.

Next in Line 5, findMaximum is called with two parameters – this time with temporary (which is the maximum of num1 and num2) and num3. Calling findMaximum twice fulfils the same task as that done by the findMaxOfThree function.

### Rules for defining functions in Java

When defining functions in Java:

• You cannot declare more than one function with the same signature – that is the same function name and same number and types of parameters
• You cannot define two functions with the same signature with different return type because the return type is not considered by the compiler when differentiating functions.

This tutorial looked at the basics of functions. We covered how to define functions and how to call them in a program.

Great stuff! The next activity is a quiz which will help you to check how well you understood the concepts.