Computer Science, asked by LissaD, 1 year ago

how to use return statement in a java program?

Answers

Answered by RohitDeshmukh
0
Return. A stone is thrown into the air. It comes back down to the ocean and makes a splash. It returns. In Java too methods return.



In a return statement, we evaluate expressions—and as part of this evaluation, other methods may run. Types must match, or an error occurs. Code after a return may become unreachable.



Expression. We can return a single value. But often we want a more complex return statement—we use an expression after the return keyword. Here an expression that multiplies two values.

ComputeSize:

This method receives two arguments, both of type int. In the return expression, the two numbers are multiplied.

And:

The evaluated result is returned to the calling location. The expression itself is not returned, just its result.

public class Program { static int computeSize(int height, int width) { // Return an expression based on two arguments (variables). return height * width; } public static void main(String[] args) { // Assign to the result of computeSize. int result = computeSize(10, 3); System.out.println(result); } } Output 30

Return method result. In a return statement, we can invoke another method. In this example, we return the result of cube() when getVolume() returns.

And:

The cube method itself returns the result of Math.pow, a built-in mathematics method.



Java program that calls method in return statement public class Program { static int cube(int value) { // Return number to the power of 3. return (int) Math.pow(value, 3); } static int getVolume(int size) { // Return cubed number. return cube(size); } public static void main(String[] args) { // Assign to the return value of getVolume. int volume = getVolume(2); System.out.println(volume); } } Output 8

Return, void method. In a void method, an implicit (hidden) return is always at the end of the method. But we can specify a return statement (with no argument) to have an early exit.

Here:

If the "password" argument has a length greater than or equal to 5, we return early. Otherwise we print a warning message.



Java program that uses return statement, void method public class Program { static void displayPassword(String password) { // Write the password to the console. System.out.println("Password: " + password); // Return if our password is long enough. if (password.length() >= 5) { return; } System.out.println("Password too short!"); // An implicit return is here. } public static void main(String[] args) { displayPassword("furball"); displayPassword("cat"); } } Output Password: furball Password: cat Password too short!

Boolean. We can use an expression to compose a boolean return value. This is a powerful technique—we combine several branches of logic into a single statement.

Boolean

And:

The result of isValid is a boolean. Both logical conditions must be satisfied for isValid to return true.



Java program that returns boolean from method public class Program { static boolean isValid(String name, boolean exists) { // Return a boolean based on the two arguments. return name.length() >= 3 && exists; } public static void main(String[] args) { // Test the results of the isValid method. System.out.println(isValid("green", true)); System.out.println(isValid("", true)); System.out.println(isValid("orchard", false)); } } Output true false false

Compilation error. A method that is supposed to return a value (like an int) must return that value. Otherwise a helpful compilation error occurs.



Java program that causes compilation error public class Program { static int getResult(String id) { // This method does not compile. // ... It must return an int. if (id.length() <= 4) { System.out.println("Short"); } } public static void main(String[] args) { int result = getResult("cat"); System.out.println(result); } }


Answered by Anonymous
1

Answer:

• return statement should be the last statement of the function.

• A function can not return two statements. (logically one return statement only to get executed)

• We cannot return two values’ return a, b

• We can return expression return (a*b+c)

Explanation:

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function. For more information, see Return type.

Similar questions