Programming with Alice. Computing Institute for K-12 Teachers Summer 2011 Workshop. Session 5. Methods and Functions.
PowerPoint Slideshow about ' Programming with Alice' - dea
An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Often, in programming, you will encounter sections of code that you will want to use more than once. In these cases it is extremely helpful to place the code in a container called a method or a function. This allows the group of blocks to be ran or “called” using only a single block.
Methods and functions serve two main purposes in programming: to simplify code and make it easier to change (see the presentation notes for a detailed explanation of these benefits).
Methods and functions, while identical in other programming languages, do have one distinct difference in Alice; methods are a collection of blocks that do things, while functions are a collection of blocks that return a value. Functions do not directly affect the objects in the world.
Drag another brightness block to the strobe method just below the previous one, only this time set the brightness to 1.
If the method was called right now it would turn the light off and then turn it back on once. To make it alternate continuously, we’re going to use an infinite loop and place both blocks inside.
The strobe is probably slower than you expect; we need a way of easily changing the speed or delay between the on and off states of the light.
With the strobe method open, click “create new parameter” on the right side of the window, name it “delay”, set type to number, and click ok. Set the duration of both set brightness blocks to the delay parameter by clicking more, mouse-over duration, mouse-over expressions, and click delay.
Functions are used to calculate and return a value.
A real life example of a function would be finding the area of a rectangle. To find the area of a rectangle, you first need two values: the length and the width. Once those values are known, simply multiply them together and the result is the area. This task could easily be made into a function called “findAreaOfRectangle” and would expect two parameters called “length” and “width.” inside the body of the function, the two parameter variables would be multiplied and stored in a third variable called “area.” The function would then return the value of the area back to the location that the function was called from.
Although most functions will make use of parameters, they aren’t always necessary as evidenced by the existence of default Alice functions such as “mouse distance from left edge” or “random number.”
Since functions must return something, they must return a specific type of something. If we created a function that added two numbers and returned the result, we would expect it to return a number, not a string, Boolean, or other value.
Functions can not affect the world directly. You can not call methods from functions but you can call functions from methods. Functions exist only to return a value.
In session 0, you created a car that moved across the screen while it’s wheels rotated. Now, we’re going to rewrite that program using methods and functions.
If you can’t find your saved project from session 0, please go back and recreate it. It’s small and, hopefully, shouldn’t take too long.
First, we’re going to create a method called “drive” for the car. Make sure the car is selected then create the method. Using the clipboard, move all the blocks to this new method and replace “world.myfirstmethod” with a call to the drive method in the Events area.
We want to be able to control the speed and distance traveled by the car so let’s add those as parameters to the drive method. With the drive method open in the method editor, click the “create new parameter” button on the right side. Both parameters will be numbers (speed and distance) so create both of type Number.
Replace the number of meters in the move block with the distance parameter from the top of the method editor.
In order to determine how many times the wheels should rotate for the given distance, we need to first find the circumference of the wheel. If we remember that circumference is pi * diameter, this will be easy to calculate.
By selecting one of the wheels, you can access a function called “object’s width” or “object’s height.” Since a wheel is round, either of these functions should return a value that will suffice for the diameter (these number are not exact which may lead to inaccurate wheel rotation speed).
Now that we have the circumference of the wheel, we will now be able to calculate the total number of rotations.
Now that the function is complete, we can use it in the drive method. Place the new angular distance function in the revolutions area of all four wheel turn methods. Set the distance parameter of the function to the distance variable in the drive method. This finished drive method should look similar to this:
By changing the parameters in the drive method call in the Events area, we can easily affect the vehicle’s movements.