OneBite.Dev - Coding blog in a bite size

declare a function with return value in java

Code snippet on how to declare a function with return value in java

public double multiplyTwoNumbers(double a, double b) {
  double result = a * b;
  return result;
}

This code declares a function named “multiplyTwoNumbers” in Java which takes two double values (a and b) as parameters. The function will return a double value, in this case the double result of a multiplied by b. Inside the function, the double result is declared as the product of a and b, then it is returned to the caller (or whatever statement called the function). The return statement halts the execution of the function, meaning that any code after the return statement will not be executed.

java