Functions in Java

November 19, 2022
Cover image for Functions in Java

Functions, or methods, in programming languages allow you to reuse code, making things more readable and maintainable. Instead of having to rewrite a bunch of code again, we can just call a function that will execute a block of code. Using functions allows code to be reused throughout an entire program. This is especially useful when working on programs that span many files. Just calling one function allows for more maintainability because you only need to modify in one place to make changes across the entire program.

Function Concepts

To write a function in Java, you need to specify:

  • Access Modifier
  • Return Type
  • Name
  • Parameters
  • Body

Access Modifier

Since Java is an Object-Oriented Programming Language, a class is declared that contains the functions to be called. The access modifiers that can be chosen are: private, public, and protected. For the purpose of this post, we'll use the public keyword which means that instances can call the function. There is also a static keyword, which is what is used in running the main method. We'll use that here as well.

public class Main {
public static void exampleFunction() {
// ...
}
}

Return Type

The return type is used to indicate what data function should send back. For example, if we expect an integer to come back the return type would be int or Integer. If we expect a string to come back the return type would be String.

public class Main {
public static int functionThatReturnsInt() {
// ...
}
public static String functionThatReturnsString() {
// ...
}
}

If we don't expect any data to come back, then the return type would be void.

public class Main {
public static void functionThatReturnsNothing() {
// ...
}
}

Name

In order to call a function in other parts of a program, you need to specify the function name.

Let's consider the previous example. In the main method, we can call the function by referencing the name functionThatReturnsNothing.

public class Main {
public static void main(String[] args) {
functionThatReturnsNothing();
}
public static void functionThatReturnsNothing() {
// ...
}
}

Parameters

Parameters are data that we can pass into a function call. Depending on the data passed in, the function can produce different results.

Say we want to have a function that prints a "Hello" message. When the function is passed, we have to give it a String value.

public class Main {
public static void main(String[] args) {
printHelloMessage("World");
}
public static void printHelloMessage(String text) {
// ...
}
}

We can specify no parameters, or as many parameters as we want. It's generally a good practice to not pass in too many parameters in order to keep the function easier to understand.

Body

The function body contains the code that will be executed when the function is called. Using the previous example, the function body could look like:

public class Main {
public static void main(String[] args) {
printHelloMessage("World");
}
public static void printHelloMessage(String text) {
System.out.println("Hello " + text);
}
}

When the printHelloMessage function is called, the parameter text gets the value "World", and then "Hello World" gets printed out to the console.

Conclusion

Functions allow code to be split up into smaller pieces, making code easier to understand and maintain. If functions are written well, then changes can take place in one location rather than many. As you write code, functions are a great place to start when it comes to making code more readable.