PDE - What's a Return? (Lesson)
What's A Return
Returning Data to a Calling Statement
Thus far we have used the Procedures in Alice which take in parameters but do not provide any return of data (information). There are two types of methods in Alice, the Procedures that allow us to pass information in, do something, and when finished the program proceeds to the next statement after the procedure call. The other type of method is the Function. You have used this method to get information from the user, and getDistance to an object, or get a part of an object to create some motion. We have not as of yet created a Function unless you have experimented with it.
We will add to the Load Random program from earlier to create get the average of numbers. Not the difference here as to where to put the average function. I have placed a getAverage at the Scene level and a getArrayAverage at the Biped level. There is a hierarchy difference here for availability just as in the procedures. If I place the getAverage at the Scene level, all objects can get to the function, whereas if I place the getArrayAverage at the Biped level, only Bipeds are able to use the function.
Notice the big red stop sign at the bottom. Return Statement Required! Look across the bottom tray. Now there is a return statement to drag in. The return statement means that data from the function must be returned back to the calling statement as a result.
When you called the getDistance function earlier in the year, the distance between two objects became known and was then used by your code to move an object. When you ask for input from the user with the getUserInput, a GUI (Graphical User Interface) box appears and waits for the user to type in data and enter it into the program. Code in the program that you wrote picked up the data and used it as coded. If a getAverage is called and code is executed, the average of some numbers should be returned back or if getArrayAveragee is used, the average of an array of numbers should be returned.
Below is an addition made to the Load Random program seen earlier to allow for the calculation of average. Average is not always a whole number answer, so a decimal number array is added to allow the user to still enter whole numbers. Note the movement of the original whole number randomArray to the decimal array, moving one number at a time using the new(DecimalNumber) conversion method. This allows the outside of the program that the user sees to still remain as a whole number entry.
After the conversion to the decimal array, not the call to the geArrayAverage function. This call is made by a Biped as it must be since only Bipeds can reach this code. If the code had been placed in the getAverage function above, any object could have made the call to the function, even a rock.
Note the call passes two parameters, the array which consists of the numbers to be averaged and the TotalNbr. Remember in the case that was coded in the Load Random page, the TotalNbr was received from the user, indicating the number of items the array should be made up of. The wholeNumber array was then loaded with the appropriate number of random numbers beginning with index 0.
Note that the call to the getArrayAverage is also an assignment statement with a variable on the left side. The datatype for the variable could be defined as shown or the variable could have been defined earlier and the DecimalNumber datatype name would not then be in this statement.
Once the information is returned to the calling statement, the next statement then uses the variable to give the information back to the user through the Alien speech.
Option 1: Using count loop to move the whole number array to a decimal array index by index.
Option 2: Using a while loop to move the whole number array to a decimal array index by index.
The getArrayAverage function of the Biped class is here.
Note:
- The parameters have a decimal array as input and the number of numbers used in the array totNbr as input. Output of this function method is the average of the numbers in the array.
- A while is used here. A count also could have been used with the same indexA < arrayUserLength condition of truth.
- Here the indexA is used to avoid confusion with the index in the myFirstMethod.
- The commented out portion shown here is an optional way to return the average data.
Video of the Run with the getArrayAverage in Use
This uses the front end with a whole number array to an decimal array first, which then we can get the average in the getArrayAverage function. The function returns the output data of the average to the calling statement variable, arrayAverage. Then arrayAverage is used to output the average data to the use on the screen.
IMAGES & VIDEO CREATED BY GAVS..