Creating your Function What will we learn today

  • Slides: 13
Download presentation
Creating your Function

Creating your Function

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

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

Functions We use functions to ask questions about conditions/objects or to compute a value

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

Functions that don’t exist in Alice We can write our own functions Our functions

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 Fun Return output

Example-Rolling a Ball Forward Simulate rolling a ball forward Think about how the ball

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!

Example-Rolling a Ball Forward Guess#2: to roll, the ball must turn and move in

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

The Solution-Rolling a Ball Forward The solution is to use “as. Seen. By=ground” in

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

How Many Revolutions? How many revolutions the ball needs to turn in covering a

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)

Writing a New Function Write a function for the toyball object to compute the

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 “number. Of. Revolutions” and select type “number”

Writing a New Function A click on the “Ok” button creates an editor panel

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

The Return Statement Each function should have a return statement because all functions return

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 Number of revolutions = distance / (diameter * ) Drag the ‘distance’ tile from the functions header and place it in the return statement Edit the return statement as follows

Calling the Function Now, the ball has a function called “number. Of. Revolutions” This

Calling the Function Now, the ball has a function called “number. Of. Revolutions” This function will be used in our world Bick an arbitrary distance of 7 meters for both move forward function and turn function

Let’s Work with Alice A Boy and a Hamster in Circus Act Create a

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