answersLogoWhite

0

When is a return statement required in a method?

Updated: 8/20/2019
User Avatar

Wiki User

11y ago

Best Answer

1) When you want to end the method prematurely for any reason. However, it is usually recommended to have a single exit point in a method, so you may want to avoid this.

2) When the method produces a result, which must be returned to the calling program.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When is a return statement required in a method?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the purpose of a method statement and what is required of the operator?

The purpose of a method statement is that it provides us with the details of what the required operator is supposed to do.


What is the purpose of method statement and what is required of the operator?

The purpose of a method statement is that it provides us with the details of what the required operator is supposed to do.


Difference in using a method that return value and method that does not return value in object orinted programming?

A method that return a value should have a return statement. The method signature should indicate the type of return value. While in the case of a method that does not return a value should not have a return statement and in the signature, the return type is void. When using a method that doesn't return a value, a programmer can not get a value from that function, but instead, it can only change variable values and run other methods.


Why use void in main of java?

No. void is not a data type. It is mandatory for all java methods to return something and if it is not going to return anything, we have to mark the method with a "void" return type to let the JVM know that it must not expect anything from the method.


What is wrong with not writing a return statement in value-returning method in java?

It is a syntax error, because a value returning method must return a value, and not writing a return statement with a value is tantamount to returning without a value.


Can return statement be in a void method in java?

the main method in java is the client code therefore doesn't return any values Unlike languages like C/C++, the user doesn't specify an error return code by returning from the main method. Instead they should use System.exit(code) to do this. If the Java main method returns, the default code of zero is returned.


What is a safety method statement?

A safety method statement is a form which is required to be filled in prior to building or maintenance work being carried out. It is often referred to as just a work method statement. It is basically a document that outlines a plan of action to perform a task safely. You can download one at http://www.geze.co.uk


How does return statement work in vb?

No Return statement in VB programming


What are return statements in java?

you can understand what is the purpose of return in java after this simple example Basically return use in java to return some values from a method note: this sample will work on all java versions, i test it on JDK 1.4 enjoy the code. file name : Class1.java ----- package mypackage1; //package name public class Class1 //class name + file name the { public String add(int a,int b) //a method in the class; its type is String and it take a and b variables; a and b are int { int answer = a+b; //this is some code in the class; maybe some operations; no need to know whats happening in this method System.out.println("I'm in Class1"); //this is some code in the class; maybe some operations; no need to know whats happening in this method return " I'm the returned value "+ answer; ////this is what you need to take from the method } public static void main(String [] arg) { Class1 c1 = new Class1(); //creating c1 object from Class1 Class System.out.println(c1.add(10,6)); //printing the returned values from the method; } } ----------------- output: I'am in Class1 I'am the returned value 16


Is 'void' a data type in java?

No. The void keyword is used to signify that a method will not return any objects. For example, if you type in "double", you'll need a return statement that has a double. So "void" means that there will be nothing returned.


When using the net present value method for evaluating an investment an increase in the required rate of return will?

The increase in rate of return will make the investment more difficult to be accepted.


What does missing return statement mean?

The compilation error occurred when a FUNCTION (a method that returns a thing, not the void-type) has at least one branch of codes does not return the required object in the correct type.It maybe confusing with some languages require to use the name of the Function as the "local" variable to be assigned with value, and hence establish the "return", while some languages, such as C# and Java, do not.---- C# ----public int My_Int_Function() {// DO NOT Use the commented statement// My_Int_Function = 1;// Instead, use a local variable that is NOT the same as the method namereturn 1; // never use the function name as the variable}