Lesson: Returning Data

Overview: 
Explore how data can be returned from a method 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 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 we are not asking the code to 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 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.

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 or "return' 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.

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.

 

Lesson navigation: