Download presentation
Presentation is loading. Please wait.
Published byBritney Wilkinson Modified over 9 years ago
1
Variables and Functions Chapter 3 3-1
2
Variables Named storage location in computer’s memory Programs may need to store data when running Types of data that can be stored o Local variables o Parameter variables o Class-level variables o World-level variables 3-2
3
Variable Types Local variables o Belongs to a specific method o Only used in that method o Variables cease to exist when method stops o Will be talked about today Parameter variables o Holds argument that is passed to a called method Class-level variables o It is like a property and belongs to a specific object World-level variables o It is a world-level property 3-3
4
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 things o Name o Type o Initial value 3-4 name type value
5
Variable Definition Variable Names o Must be unique within the method or object o Should be meaningful and reflect the variable’s purpose o Named using camelCase Variable Types o Numbers, Boolean, Objects, Other (such as String, Color, Sound, etc.) Initial Value o Value initially stored in the variable 3-5
6
Variables Tile appearance shows the type of variable Numbers Boolean Objects String Variable declaration - creating a variable 3-6 variable area
7
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 running o Use Set instructions o Done by dragging variable tile into Methods Editor 3-7
8
Review What is a variable? What are three things that must be specified when creating a new variable? Where in the Method Editor do variable tiles appear? What do the characters 123 appearing on a variable tile tell you? What do characters abc on a variable tile tell you? How do you create a set instruction for a variable? 3-8
9
Function Asks a question to o Check a condition o Compute a value Used to get information about… o Size of objects What is snowwoman's height? o Relationship of one object to another What is distance between lunar robot and rock? Used to get other information o Time o Ask user for information
10
Value What a function returns after it o Asks question o Or performs a computation Type of value depends on kind of function o distance to returns a number o is to the left of returns a Boolean value Number value o Whole number ex: 3 meters o Fractional value, ex: 1.2 meters o In lunar example What is distance of robot to rock?
11
Function vs. Method Functions o Perform calculations o Determine state of an object or system o Return a value Methods perform an action 3-11
12
Types of Values Number o 3, or 1.3 Boolean o true, false String o “Slithy Toves?” Object o lunarRobot, alien Position in world o (0, 0, 0) – (x,y,z) center of an Alice world
13
World Level Primitive Functions List of world level functions categories o Boolean logic o Math o Random o String o Ask user o Mouse o Time o Advanced math o Other 3-13
14
Asking User for Input World has three primitive functions that ask for user input Each question is shown in a dialog box Ask user for a number User enters a number Ask user for yes or no User clicks on yes or no Ask user for a string User answers with words or a sentence 3-14
15
Adding Question for Number
16
Place Answer in Variable 3-16
17
Class Primitive Object Functions Objects have different primitive functions from the world level Class level functions o Proximity o Size o Spatial relation o Point of view o Other Object primitive proximity functions
18
Using Function distance to Built-in distance to function o Determines distance from center of one object to center of another object o Better than guessing distance to object An Alice function is not a "stand-alone" instruction o It is nested within another instruction
19
Review What makes a function different from a method? How do you see a list of 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-19
20
Homework Read chapter 3 Do tutorials o Tutorial 3-1 Creating and using a variable, p 116 o Tutorial 3-2, Creating a set instruction, p. 118 o Tutorial 3-3, Calling an ask user function, p. 122 o Tutorial 3-4, Using a proximity function, p. 127 Do lab assignments Due one week after its assigned
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.