Title: Variables and Functions
1Variables and Functions
2Naming is Important
- If you get a new pet one of the first things you
do is name it - Gives you a way to refer to the new pet without
saying - Please take that dog we got yesterday for a walk.
- Please take Fifi for a walk.
- In programming we name things we want to refer to
again - Gives us a way to work with them
- Like the World object
- This is called declaring a variable
3Declaring a Variable
- To be able to refer to an object again we need to
specify what type of thing it is and give it a
name - This is also called declaring a variable
- Type name OR
- Type name new Class(value, value, )
- The equal sign doesnt mean equal
- But assign the value of the variable on the left
to the result of the stuff on the right - The following creates a variable named earth
which refers to a World object created on the
right - World earth new World()
4Declaring Variables
- Variables are names associated with values
- If the type of the variable is null
- It doesnt refer to an object yet
- Variables can be reused
- World earth null
- earth new World()
- earth new World()
null
earth
World Object 1
earth
earth
World Object 2
5A Variable Associates a Name with Space
- A variable is like a box with a label on it
- You can put something in a box
- You can take something out of a box
- You can even change what is in the box
- The size of the box restricts what you can put in
it
Hat Box
6Limits on Declaring Variables
- You can't declare two variables with the same
name! - gt World earth new World()
- gt World earth new World()
- Error Redefinition of 'earth'
- You can change what an object variable refers to
- gt World earth new World()
- gt earth new World()
7Declaring Variables and Creating Objects
- You can declare a variable and assign it to refer
to a new object in one statement - World earth1 new World()
- Turtle tommy new Turtle(earth1)
Declaration of variables
Creating the objects
8Storing a Value in Variable x
- Alice
- x.set( value, something )?
- Java
- x something
- object
- number
- string
- some computation
9Functions in Alice
- A method in Alice is called a void function in
other languages, because it returns nothing. - Returns? What's returns?
- A non-void function returns information
- In Alice, this is a function
10How a function works
- A function receives value(s), performs some
computation on the value(s), and returns (sends
back) a value.
11Types of Functions
- The type of a function depends on the type of
value it returns. - a number
- a specific object
- a color
- a Boolean (true or false)?
- other property values
12SumOfSquares Function
- The sumOfSquares function receives two value(s) x
and y, performs the computation x2 y2 on these
value(s), and returns (sends back) this result.
X 3, y 5
x 1, y 4
x2 y2
34
17
13SumFrom1To Function
- The sumFrom1To function receives a value N, and
performs the computation 1 2 N, and
returns this sum.
N 3
N 100
1 2 N
6
5050
14Using a Built-in Function
- The distanceTo( ) dest_object function receives
an object and determines the distance from the
dest_object and the object and sends the value
back to the caller.
cowboy
distanceTosheriff (this)?
1.3
15(No Transcript)
16Give Function Demo
17A new example
- A common task in sports game programs is to
bounce a ball. To illustrate how this is done,
let's bounce the ball over the net. (The ball has
been positioned 1 meter from the net.)? - The ball should move up and forward and then down
and forward in a pattern that looks something
like this - Note This looks easy but do not be
deceived!
18Design Storyboard
- A design for a World-level method
World.ballOverNet Do in order toyball turn
to face the net Do together toyball
move up toyball move forward Do
together toyball move down toyball
move forward
19Demo
- Concepts illustrated in this example
- Movement of an object is sometimes relative to
another object. In this example, - a built-in height function is used to determine
the height of the net. Then the ball moves up
enough to clear the net. - the toyball's up and down movements are relative
to the ground. An orient To method
(alternatively stand Up) is used because it is
not easy to tell "which way is up."
20Rolling the ball
- Another 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.
realisticRoll Do together move ball forward 1
meter turn ball forward 1 revolution
21Demo
- 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.
22Revising the approach
- The ball is made to roll 1 revolution. Suppose
we want the ball to roll a certain distance (say
3 or 4 or even 10 meters) along the ground. - How many times should the ball turn 1 complete
revolution?
23(No Transcript)
24Parameters
- We want to return the value computed as
- distance/( ? diameter)?
- Obviously, what is needed is
- the balls diameter
- the ball object has a built-in width function
- the distance the ball is to travel
- can be sent as a parameter to the function
25Demo
- Concepts illustrated in this example
- A function must have a return statement to send
back a computed value. - In this example, a math expression is created to
compute a number value. - The order of evaluation is indicated by the use
of parentheses, as in traditional math
expressions.
26Calling the function
test values We should run the
animation with several test values to be sure it
works as expected. What happens if you use a
negative value?
27Levels of Functions
- As with methods, functions can be either
class-level or world-level. (The function just
presented was class-level.)? - The guidelines for class-level methods also apply
to class-level functions - No references to other objects.
- No references to world-level functions you have
written (built-in world-level functions are fine
to use).