Writing Our Own Functions Alice. Functionality A function receives value(s), performs some...
-
Upload
alexis-ferguson -
Category
Documents
-
view
212 -
download
0
Transcript of Writing Our Own Functions Alice. Functionality A function receives value(s), performs some...
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.
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
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
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.
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!
Number of revolutions
• The number of revolutions depends on the size of the ball.
one revolution
four revolutions
distance
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
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
Parameters
• We want to return the value computed as distance / (Pi * diameter)
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)
To write a new function
• Click on functions (Functions) Tab.• Choose “Create new function”
numberOfRevolutions
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.
numberOfRevolutions FunctionYour function should have this information.
Test your new function.
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?
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).
Now an exercise…