Skip to content

Functions

Chris Ward edited this page Oct 7, 2022 · 3 revisions

Have you been noticing that the concepts we learned can carry over to Java? A lot of learning a different language after picking up another one is learning the syntax. We covered functions in Python too, and the concept of them is the same. It is a small block of code that we want to be able to call sometimes more than once. Sometimes they take arguments, or parameters, and sometimes they return a value. Lets look at a simple function in Java.

package examples.d_function;

public class Function {
    public static void printHello() {
        System.out.println("Hello");
    }

    public static void printStuff(String stuff) {
        System.out.println(stuff);
    }

    public static void main(String[] args) throws Exception {
        printHello();
        printStuff("Hello World");
        printStuff("Programming is cool");
    }
}

For now, when we call our functions directly from main, which is static, we need to call our functions static too.

Here we have 2 functions. The first function doesn't have any parameters, and it doesn't return anything. Another way to say noithing, is a void. We didn't have to tell Python if we returned something or not, but Java wants to know if we do. We will look more at returning stuff in a minute.

Just like in Python we can call functions with different parameters. In the printStuff function we print to the console the string we pass to the function. Notice that we also had to tell the function the type we were passing in. We didn't have to tell Python that either. This is why Java is called a strongly typed language, and Python is a loosely typed language.

Can you change the code to print your message 5 times? Are there 2 different places you could put your loop? Why would you choose the different places?

Can you change the code to accept a name, such as "Sarah", and then print "Hello Sarah"? Hint: println prints a whole line, print doesn't drop to the next line.

Returning stuff

In Java we always have to tell our function what it is returning. Even when we didn't return anything we had to tell it we didn't return anything by calling it void.

To return an int, we say public int returnNumber() To return a String we say public String returnString()

Lets take a look at the example to add some numbers.

package examples.d_function;

public class Function2 {
    public static int add(int one, int two) {
        return one + two;
    }

    public static void main(String[] args) throws Exception {
        int value = 0;

        value = add(3, 7);
        System.out.println(value);

        value = add(23, 24);
        System.out.println(value);
    }
}

The add function is now defining a return type of int, and two parameters that are both int. This function will behave just like a Python function would, but we just have to tell the compiler about the types.

The main method is just using this add method to demonstrate.

Could you make other methods to subtract, multiply, and divide? Make these methods, then see if you can do ((3 + 7) / 2 - 1) * 2 = 8. Yes, I am aware that you can just do the math without functions, but lets explore functions here.

Now lets take a bit more of a look at strings.

Clone this wiki locally