Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating your Function

Similar presentations


Presentation on theme: "Creating your Function"— Presentation transcript:

1 Creating your Function
Note to Instructor: Slides may be used either in a traditional lecture format or with an Active Learning approach. If you are using an active learning approach, suggestions will appear in this "Notes" area on appropriate slides.

2 What will we learn today?
We will learn Functions Write your own function

3 Functions We use functions to ask questions about conditions/objects or to compute a value Unlike methods, functions don’t change the state of the world Alice provides a list of built-in functions that are useful in methods

4 Functions that don’t exist in Alice
We can write our own functions Our functions may receive values sent in (input), perform computation on values and return (send back) Input value Return output Return types..Numbers, Strings and Booleans Fun

5 Example-Rolling a Ball Forward
Simulate rolling a ball forward Think about how the ball can be made to roll along the ground Guess #1: turn instruction It simply rotates the ball in place!

6 Example-Rolling a Ball Forward
Guess#2: to roll, the ball must turn and move in the same direction and at the same time Testing of this code is disappointing The ball ends up at the same place it started (Turn action prevents the ball from moving forward) Do together move ball forward 1 meter turn ball forward 1 revolution Why?? Because the ball is moving relative to itself and not relative to the ground

7 The Solution-Rolling a Ball Forward
The solution is to use “asSeenBy=ground” in the move statement Do together move ball forward 1 meter asSeenBy=ground turn ball forward 1 revolution

8 How Many Revolutions? How many revolutions the ball needs to turn in covering a given distance in a forward direction? 1 revolution will not be realistic Challenging!! The number of revolutions is proportional to ball’s diameter (a small ball needs more turns than a big one to cover the same distance) A Big ball covers same distance in one revolution as the small ball covers in 4 revolutions

9 Writing a New Function Write a function for the toyball object to compute the number of revolutions needed Select the objects “toyball” in object tree In function tab, click on the “create new function” button Function box appears Enter name of function “numberOfRevolutions” and select type “number”

10 Writing a New Function A click on the “Ok” button creates an editor panel Create a parameter “distance” for our function Click on the “create new parameter” button at the right side of the screen A “create a new parameter” window will appear

11 The Return Statement Each function should have a return statement because all functions return information The return statement Cannot be removed The number of revolutions depends on distance traveled by the (circumstance) of the ball in a single revolution Drag the ‘distance’ tile from the functions header and place it in the return statement Edit the return statement as follows Number of revolutions = distance / (diameter *  ) The Return statement cannot be removed A parameter will be used to send in the distance the ball is to roll The diameter is the built-in function “toyball’s width” The symbol  (pi) is the constant 3.14

12 Calling the Function Now, the ball has a function called “numberOfRevolutions” This function will be used in our world Bick an arbitrary distance of 7 meters for both move forward function and turn function We should do testing to functions with small numbers (-2 to 0)

13 Let’s Work with Alice A Boy and a Hamster in Circus Act
Create a new world as shown in the picture below, position the two objects at the top of the rolling ball Write a program for a circus act, where the boy and hamster move with the ball, staying on top of it as the ball rolls Create a method for the boy to move with the rolling ball while moving his legs back and forth gently, and putting his arms up halfway to help him balance as he moves along with the ball For an advanced work, make the 3 objects do a complete revolution while the boy and the hamster staying on top of the ball


Download ppt "Creating your Function"

Similar presentations


Ads by Google