Core elements of programs - Functions

Introduction

Functions let you group bits of code together, such that it completes one single function. You’ve actually already been using functions, you just haven’t known it yet. Functions mean that you don’t have to repeat the same block of code multiple times, and they let you greatly increase the complexity of your program. Functions really increase the functionality of your program (Ahahahahahahahaha).

Syntax

public class Main
{
	public static void main(String[] args) {
		printPotato(); //Runs the code inside printPotato
	}
	
	public static void printPotato() {
		//Code that gets run by printPotato goes here
		System.out.println("Potato.🥔");
	}
}

This get’s placed within the class body (i.e. after “class Whatever”)

Notice that, like variables, function declerations have various keywords that change how they work. Let’s deconstruct the above decleration!

So note that you don’t always have to have all of those different keywords. (You do always need the void however.) So for example you could have:

static void printPotato() {

or even

void printPotato() {

…Although that last won won’t work in this instance if you try it… basically as the main function (which is a special function that gets run when the program starts) is static, it only likes to directly call other static functions. If there wasn’t the “static”, it would only be happy calling that function if it was calling it from an object… which will be covered more in Object Orineted Programming (OOP).

You might start to be realising that a lot of things only make sense when you know OOP. This is because Java is an Object-Orineted-Programming language, and so heaps of design decisions were made around that. Don’t worry, you’ll learn it soon enough.

Variables

One very useful feature of functions is that they can recieve and return variables. So your function can take some variables, do something with it, and then give it back to the code that called it. For example:

static int cube(int numberToCube) {
	return numberToCube*numberToCube*numberToCube;
}

Then somewhere else in the code:

int num1 = 4;
int num2 = 839;

int num1Cubed = cube(num1);

System.out.println("Number1: " + num1 + "  Cubed: " + num1Cubed);
System.out.println("Number2: " + num2 + "  Cubed: " + cube(num2));

Which will produce…

Number1: 4  Cubed: 64
Number2: 839  Cubed: 590589719

So, an explanation! When defining a variable, inside the brackets you can define variables to recieve information when that function is called, like:

public static void example(String aString, int aNumber, double aDouble, boolean aBoolean) {

Every VariableType variableName pair is seperated by a coma, and when the function is run, that variable is set to value of whatever was passed into it.

The void bit dosen’t actually have to be void. It defines what the function will return. If it is void, it says the function won’t return anything. BUT it could be any type of variable.

The return keyword ends the running of the function and returns the value that comes after it back to whatever called the function.

So the function

public static String yayString() {
	return "I am LORD POTATO";
	System.out.println("Yay lord potato has returned");
	return "all praise lord potato";
}

would never get past the first line, and so never print out it’s message or return the second message, as it stops running on the first line as that is a return line.

Activities

  1. Write a function that finds the average of 4 numbers. Use the function to print out the results (NOTE: The function should NOT print out the result.)
  2. Write a function which, given the starting amount, interest rate/year and amount of years returns the final amount of money. You can use the formula “final amount = starting amount * (1+interest rate)years accumlating” to calculate it. (Assume that it compounds yearly.)
  3. Write a function that returns a boolean, checking if the given number is prime.