Writing Our Own Functions Alice. Functionality A function receives value(s), performs some...

18
Writing Our Own Functions Alice

Transcript of Writing Our Own Functions Alice. Functionality A function receives value(s), performs some...

Page 1: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Writing Our Own Functions

Alice

Page 2: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Functionality

• A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Page 3: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Types of functions

• The type of a function depends on the type of value it returns.– a calculated value (a number)– a Boolean– a specific object– a color– etc.

Page 4: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Built-in functions

• We have used Alice's built-in functions for many purposes skateAround method for the cleverSkater.

• Let's take a look at another example.

Returns a number

Page 5: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Writing our own functions

• A common sports game action involves rolling a ball along the ground.

• We want a realistic motion rather than a slide. • The ball must simultaneously move and roll.

toyBall.realisticRoll

Do together move ball forward 1 meter turn ball forward 1 revolution

Page 6: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

What happens?

• Our design assumed that the ball's motion is relative to the ground. But the ball turns relative to it's own sense of direction.

• AsSeenBy ground can be used to make the ball turn relative to the ground.

Page 7: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Revising the approach

• The ball is made to roll 1 revolution. Suppose we want the ball to roll a certain distance along the ground.– Change to 3 meters-duration 3 seconds.– What happens?

• How can we make the ball roll the correct number of revolutions to cover a given distance along the ground?– Some MATH!

Page 8: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Number of revolutions

• The number of revolutions depends on the size of the ball.

one revolution

four revolutions

distance

Page 9: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Number of revolutions

• The number of revolutions depends on the size of the ball– The number of revolutions needed is

• distance/( Circumference of Ball)– Circumference of circle

• Pi * diameter – The number of revolutions needed is

distance/(Pi * diameter)

one revolution

four revolutions

distance

Page 10: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Number of revolutions

• The number of revolutions depends on the size of the ball– The number of revolutions needed is

Distance / (Circumference of Ball)

– Circumference of circlePi * diameter

– The number of revolutions needed isdistance/(Pi * diameter)

• But there is no built-in function to return the number of revolutions

• We will write our own

one revolution

four revolutions

distance

Page 11: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Parameters

• We want to return the value computed as distance / (Pi * diameter)

Page 12: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Parameters

• We want to return the value computed as distance / (Pi * diameter)

• What is the value of Pi?• What is needed?

– the ball’s diameter– Does the ball have a built-in function that

gives us this information?– the distance the ball is to travel (can be sent

as a parameter to the function)

Page 13: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

To write a new function

• Click on functions (Functions) Tab.• Choose “Create new function”

numberOfRevolutions

Page 14: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

To write a new function

Return Statement– All functions return some value.– You must keep the Return Statement in the code.– You automatically get the Return….You must

revise the return value.

Page 15: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

numberOfRevolutions FunctionYour function should have this information.

Test your new function.

Page 16: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Demo: Calling the function

We should run the animation with several test values to be sure it works as expected.

What happens if you use a negative value?

Page 17: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Levels of functions• As with methods, you can write functions

(functions) as either class-level or world-level.

• The guidelines for class-level methods also apply to class-level functions (functions):– No references to other objects.– No references to world-level functions you have

written (built-in world-level functions are fine to use).

Page 18: Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Now an exercise…