5.3. Method Returns¶
A method bundles together lines of code that perform a specific task.
You can pass values into a method using formal parameters.
You can pass a value out of a method using a return statement.
You saw examples of methods that return values in Unit 2 with the Turtle
methods (getWidth, getXpos, etc).
The method calls were usually on the right hand side of an assignment, or they were contained in a print statement.
When a method returns a value, the code should do something with the value such as store it in a
variable or print it.
You will learn how to create methods that access object attributes in a later lesson. This lesson shows you how to create static methods that are functions. A function takes one or more values passed as formal parameters and computes a new value to return.
A method can return at most one value. The method signature specifies the return type, which can be a primitive (int, double, boolean), a class (String, Turtle, etc), or void.
- public
- This is the access modifier.
- static
- This is the non-access modifier.
- double
- Correct. This is the return type.
- volumeCylinder
- This is the method name.
5-3-1: This method computes the volume of a cylinder. The method has formal parameters for radius and height, and returns a value representing the corresponding volume. Given the method signature, what is the return type?
5.3.1. Method Return Type¶
A void return type means the method does not return a value. If a method has a non-void return type, then it must contain a return statement that specifies the value to return. The return type must match with the value in the return statement.
Click on each tab to observe the data flowing into the method through the formal parameters and out of the method through the return statement.
The program starts at the first line of the main method. The red arrow shows that line 12 is next to execute, which will call the volumeCylinder function.
The stack diagram shows a new frame was created for the volumeCylinder(4,10) method call. The new stack frame contains the formal parameter variables initialized to the actual argument values.
After line 6 is executed, the stack frame shows the computed value 502.6 will be returned out of the method. Where does the returned value go? The call stack diagram indicates the value is returned to line 12 of the main method, since that is the line of code that called the volumeCylinder method.
The value was returned to the main method and line 12 assigns the value to the “vol” local variable. Line 13 is the next line to execute.
Line 13 prints the value stored in the vol local variable. The output window shows what was printed.
Use the CodeLens to step through the code. Experiment with passing different values to the volumeCylinder method.
The code below contains a method inchesToCentimeters
that computes and prints the centimeter equivalent of the value passed into the inches parameter.
Instead of printing the centimeter value inside the inchesToCentimeters method, you should update the
method to return the value and then move the printing to the main method. You will have to change
the return type of the inchesToCentimeters method to match the type of the value being returned.
Update the main
method to print the value returned by the inchesToCentiments
method.
- return "hello";
- The method return type int does not match the return statement type String.
- return true;
- The method return type int does not match the return statement type boolean.
- return 7.5;
- The method return type int does not match the return statement type double.
- return 10;
- The method return type int matches the return statement type int.
5-3-4: Based on the method header below, which return statement has the correct type?
public static int mystery()
- return "hello";
- The method return type boolean does not match the return statement type String.
- return true;
- The method return type boolean matches the return statement type boolean.
- return "true";
- The method return type boolean does not match the return statement type String.
- return 10;
- The method return type boolean does not match the return statement type int.
5-3-5: Based on the method header below, which return statement has the correct type?
public static boolean mystery2()
- String result = mystery3();
- The method return type int does not match the variable type String.
- int result = mystery3();
- The method return type int matches the variable type int.
- boolean result = mystery3();
- The method return type int does not match the variable type boolean.
5-3-6: Based on the method header below, which assignment statement is correct?
public static int mystery3()
- String result = mystery4();
- A void return type means no value is returned. There is no value to assign.
- int result = mystery4();
- A void return type means no value is returned. There is no value to assign.
- boolean result = mystery4();
- A void return type means no value is returned. There is no value to assign.
- mystery4();
- A void return type means no value is returned. You call the method as a statement.
5-3-7: Based on the method header below, which statement is correct for calling the method?
public static void mystery4()
- return 10;
- The method return type int matches the return statement type int.
- return 12 * 4;
- The method return type int matches the return statement type int.
- return 15 / 2;
- The method return type int matches the return statement type int.
- return 3.7 ;
- The method return type int does not match the return statement type double.
5-3-8: Based on the method header below, which return statement DOES NOT have the correct type?
public static int mystery()
A pedometer estimates that taking 2,000 steps is the same as walking 1 mile.
Write a method convertToMiles
that takes a parameter for the number of steps and returns the equivalent miles walked.
Update the main method to call convertToMiles
3 times with values 500, 2000, 3000.
Carefully consider the method return type. Watch out for integer division in the method body!
You can assume the number of steps is an integer.
Write a function randomInteger
that takes two integer
parameters min
and max
and returns a random integer value between min and max (inclusive).
Have the main method call the function with different values. You might want to go back and
review random number generation in Unit 2-9.
5.3.2. Summary¶
A method can return at most one value
The method signature must specify the return type
A void return type indicates the method does not return a value
The return statement is used to return a value
The return statement causes control to immediately transfer out of the method.