In Java, you can use the return
statement to terminate a function and return a value to the caller. When the return
statement is executed, the function terminates immediately and the control is returned to the caller.
Here's an example of how to use the return
statement to terminate a function in Java:
public int add(int a, int b) { return a + b; }
In this example, the add
function takes two integers as arguments and returns their sum. The return
statement terminates the function and returns the sum to the caller.
You can also use the return
statement to terminate a function without returning a value, by specifying the void
type for the return type of the function:
public void printMessage(String message) { System.out.println(message); return; }
In this example, the printMessage
function takes a string as an argument and prints it to the standard output. The return
statement terminates the function without returning a value.
You can find more information about the return
statement and how to use it in Java in the Java documentation.