5.3. Method Returns

A method bundles together lines of code that perform a specific task.

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.

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.

../_images/frame1.png

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.

../_images/frame2.png

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.

../_images/frame3.png

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.

../_images/frame4.png

Line 13 prints the value stored in the vol local variable. The output window shows what was printed.

../_images/frame5.png

Use the CodeLens to step through the code. Experiment with passing different values to the volumeCylinder method.

coding exercise Coding Exercise

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.

exercise Check your understanding

coding exercise Coding Exercise

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.

coding exercise Coding Exercise

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.

You have attempted of activities on this page