Variables and Functions
description
Transcript of Variables and Functions
![Page 1: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/1.jpg)
Variables and Functions
Chapter 3
3-1
![Page 2: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/2.jpg)
Variables Named storage location in computer’s
memory Programs may need to store data when
runningo Stored in a variable
Types of data that can be storedo Local variableso Parameter variableso Class-level variableso World-level variables
3-2
![Page 3: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/3.jpg)
Variable Types Local variables
o Belongs to a specific methodo Only used in that methodo Variables cease to exist when method stops
Parameter variableso Holds argument that is passed to a called
method Class-level variables
o Variable that belong to a specific object World-level variables
o Variable that belongs to the world3-3
![Page 4: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/4.jpg)
Creating Local Variables Local variables belong to
a specific method (such as world.my first method)
Create variable by clicking on create new variable in desired method
Variables require 3 thingso Nameo Typeo Initial value
3-4
name
type
value
![Page 5: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/5.jpg)
Names, Types and Initial Values• Variable Names
• Must be unique within the method• Should be meaningful and reflect the variable’s
purpose• Named using camelCase
• Variable Types• Numbers, Boolean, Objects, Other (such as
String, Color, or Sound)
• Initial Value• Value initially stored in the variable
3-5
![Page 6: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/6.jpg)
Variables
Tile appearance shows the type of variable
Numbers, Boolean, Objects, String Variable declaration - creating a variable
3-6
![Page 7: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/7.jpg)
Variable Assignment
• Variables have an initial value• Initial value held until a different value is
assigned• New values can be
assigned while the method is runningo Use Set instructionso Done by dragging variable tile into Methods
Editor
3-7
![Page 8: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/8.jpg)
Checkpoint What is a variable? What are three things that must be specified
when creating a new variable? In what part of the Method Editor do
variable tiles appear? What do the characters 123 appearing on a
variable tile tell you? How do you create a set instruction for a
variable?
3-8
![Page 9: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/9.jpg)
Function Asks a question
o Checks a conditiono Computes a value
Used to get information about…o Properties of objects
Is snowwoman's face red?
o Relationship of one object to another What is distance between lunar robot and rock?
o A current condition What keyboard key was pressed?
![Page 10: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/10.jpg)
World Level Primitive Functions List of functions categories
o Boolean logico Matho Randomo Stringo Ask usero Mouseo Timeo Advanced matho Other
3-10
![Page 11: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/11.jpg)
Value What a function returns after it
o Asks questiono Performs computation
Type of value depends on kind of functiono In lunar example
What is distance of robot to rock?
o Number value is expected to be returned Whole number ex: 3 meters Fractional value, ex: 1.2 meters
![Page 12: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/12.jpg)
Types of Values Number
o As in example Boolean
o true, false String
o “Slithy Toves” Object
o snowman, helicopter Position in world
o (0, 0, 0) – center of an Alice world
![Page 13: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/13.jpg)
Asking User for Input
World has three primitive functions that ask for user input
Each function is displayed in a dialog box
3-13
Is it Yes or No?
![Page 14: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/14.jpg)
Adding Question for Number
![Page 15: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/15.jpg)
Class Primitive Object Functions Objects have different
primitive functions from the worldo Proximityo Sizeo Spatial relationo Point of viewo Other
Object primitive proximity functions
![Page 16: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/16.jpg)
Using Function distance to
Built-in distance to function o Determines distance from center of one object
to center of another objecto Better than guessing distance to object
An Alice function is not a "stand-alone" instructiono It is nested within another instruction
![Page 17: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/17.jpg)
Checkpoint What makes a function different from a
method? How do you see a list of the functions an
object has? What are the three primitive world functions
that ask the user to input a value? What are some class level functions? What does the primitive distance to function
do?
3-17
![Page 18: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/18.jpg)
Creating Math Expressions Math Operators are used to
create math expressions Math expressions perform a
calculation and return a value Simple math expression
o 12 + 2o Value returned is 14
Operands appear to the right and left of an operatoro Example with variables “payRate” and
“hoursWorked” payRate * hoursWorked
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
3-18
![Page 19: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/19.jpg)
Determining Distance Camera angle influences our perception of scene We need assistance with determining distance Example: How many meters does robot need to walk to
reach rocks where alien is hiding?o Can use
pointOfView property Primitive function
![Page 20: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/20.jpg)
Avoiding Collisions• When program is run, robot collides with
rocko Distance between two objects is measured
center-to-center
• One way to avoid a collisiono Reduce calculated distance to objecto Use expression to subtract small number (1 or
2) from distance
![Page 21: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/21.jpg)
Expressions in Arguments Math expressions can be used in place of
o Fixed valueo To adjust value returned by function
Click down arrowto get math operation
o Adjusts distance to move to not collide with rock
![Page 22: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/22.jpg)
Adding Math Expression in Alice
3-22
![Page 23: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/23.jpg)
Subtracting Functions Two meters subtracted from distance is arbitrary To be more precise, subtract width of rock
o But still not accurate
Resulting expression subtracts value of one function from value of another function
![Page 24: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/24.jpg)
More Precision
The most accurate way to determine distance and to avoid collisiono Add half width of both objects subtract from
distance Be careful of excessive parentheses
![Page 25: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/25.jpg)
Checkpoint What do the following operators do in most
programming languages?o + - * /
What does a math expression do? When you use the move to instruction to
move one object to another object, why do they collide?o What are multiple methods you can use to avoid
this? When can an object be driven into the
ground or above the ground when using move to or move toward instructions? 3-25
![Page 26: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/26.jpg)
Working with Strings and Text
A string is a sequence of characters Strings represent
o Nameso Addresseso Messageso etc.
3-26
![Page 27: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/27.jpg)
Asking User to Enter a String World’s ask user function can ask user to enter string Function returns the text that the user enters Example displays what user entered in dialog bubble
3-27
Defines string variable called name
Asks user for name
Stores in variable name Puts contents of name variable in bubble
![Page 28: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/28.jpg)
Joining Strings
String Concatenationo Joins string a with string bo Strings a and b are argumentso Does not change the values
of strings a or b
World functiono a joined with b
Drag a joined with b from string section in World function to where you want to join strings
3-28
![Page 29: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/29.jpg)
Example of a joined with b
3-29
Hello joined with was only thing added
![Page 30: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/30.jpg)
Converting Numbers to Strings Sometimes you need to display numbers to
user Number needs to be converted to string first Use world functionWhat as a string
![Page 31: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/31.jpg)
Example of what as a string
3-31
![Page 32: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/32.jpg)
3D Text can be added to Alice worlds Found at end of the local gallery Pop up window appears after clicking
on it Enter text and font style
3-32
![Page 33: Variables and Functions](https://reader036.fdocuments.in/reader036/viewer/2022062301/56813e18550346895da7fb90/html5/thumbnails/33.jpg)
Homework Read chapter 3 Do tutorials
o Tutorial 3-1 Creating and using a variable, p 116o Tutorial 3-2, Creating a set instruction, p. 118o Tutorial 3-3, Calling an ask user function, p. 122o Tutorial 3-4, Using a proximity function, p. 127o Tutorial 3-5 Using math to avoid collisions, p 132o Tutorial 3-6, Converting a number variable to a string, p.
141 Answer questions in handout Do lab assignments after handing in answered
questions Due one week after assigned with 1 week grace