OneBite.Dev - Coding blog in a bite size

declare a simple function in java

Code snippet on how to declare a simple function in java

public static void myFunction(int a, int b) {
  int result = a + b;
  System.out.println(result);
}

This code declares a simple function named “myFunction” that takes two integer values, “a” and “b” and prints out the result of adding them together. The code starts with the keyword “public static void”, which identifies the function as public (visible to other classes) and as a static (meaning it doesn’t require an instance of its containing class in order to be used). Then the name of the function is declared (“myFunction”) followed by parentheses containing the parameters to be passed in (two integers, “a” and “b”). Inside the curly braces that follow is the code that will execute when the function is called, in this case adding the two values together, assigning the result to a variable, and then printing the result to the console.

java