Download presentation
Presentation is loading. Please wait.
Published byAlexis Ferguson Modified over 9 years ago
1
Writing Our Own Functions Alice
2
Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.
3
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.
4
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
5
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
6
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.
7
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!
8
Number of revolutions The number of revolutions depends on the size of the ball. one revolution four revolutions distance
9
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
10
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) But there is no built-in function to return the number of revolutions We will write our own one revolution four revolutions distance
11
Parameters We want to return the value computed as distance / (Pi * diameter)
12
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)
13
To write a new function Click on functions (Functions) Tab. Choose “Create new function” numberOfRevolutions
14
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.
15
numberOfRevolutions Function Your function should have this information. Test your new function.
16
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?
17
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).
18
Now an exercise…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.