Lesson: Returning Data

Overview: 
Explore how data can be returned from a method to the code that called the method.
Objectives: 

Understand returning data from a method and how this construct is used to communicate results from a method to the code that called the method.

Content: 

While methods can perform their function without returning any data to the calling code, a common use of methods is to perform some process or computation and return a result to the caller. When a method does not return a value, the return data type is called void. An example of a method that would have no return value would be a motor’s setPower(x) function. When we use that function, we aren’t looking for the code to compute something and give us the result, but we are asking the method to make some change in the robot hardware’s current running state. Void sounds like a negative connotation, but it only indicates that a method does not give us a value back. 

When we do want to return a value, the data type in front of the method name defines what kind of data we will return. The statement we use inside a method to send a data value back to the calling code is return. Return stops method execution and returns the value specified on the return statement. The data type of the value on the return statement must match the data type specified on the method definition. Here is an example:

We would call that method like  this:

This would place 2 into variable z. Note that the method does not require an input (hence the parentheses are empty). It will always return an integer value of 2. The variable x is defined inside the method so it is only visible inside the method.

We could also do this:

That would place 7 into z.

If you are returning a value, all return statements in the method must return a value of the correct data type. If the return type is void, all return statements in the method must not have a value on them.

You can return all data types including reference types (objects). Here is an example using the String data type:

We would call that method like  this:

This would print "my string" on the screen twice.

Note that a method can be called any place a data value that has the same type as the method returns. In the second println() statement, the JVM will execute the method which returns a string and then that string is passed into the println() method which can accept a string input.

When working with objects, methods can alter the internal variables of the object thus changing the object's state. This is the most common case of a method that does not return data to the caller. In this case, the result of the method's actions is the altered state of the object. This will be discussed in more detail in the unit on objects.

Here is the example in CodingGround. Add a new method that returns a double value of 22.7. Then modify the main method to call your new method and print the returned result.

A robotics example of a return value is getting information about the hardware. For example, lets say a touch sensor has a method getTouch() that returns a boolean, false if not touching anything, true if touching something. We might write this code:

Here the  sensor.getTouch() method returns a boolean, which is then tested by the if statement. A robotics example of a method with no return is controlling the behavior of a motor:

The power level is set to zero in the motor object and the method takes care of sending that power level to the actual motor on the robot.

 

Navigation: