Variables and Functions Chapter 3 3-1 Variables Named storage location in computer’s memory Programs may need to store data when running Types of data that can be stored o o o o Local variables Parameter variables Class-level variables World-level variables 3-2 Variable Types Local variables o o o Parameter variables o Holds argument that is passed to a called method Class-level variables o Belongs to a specific method Only used in that method Variables cease to exist when method stops Variable that belongs to a specific object World-level variables o Variable that belongs to the world 3-3 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 o o Name Type Initial value name type value 3-4 Variable Definition Variable Names o Must be unique within the method 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 Variables Tile appearance shows the type of variable Numbers Boolean Objects String Variable declaration - creating a variable variable area 3-6 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 o Use Set instructions Done by dragging variable tile into Methods Editor 3-7 Checkpoint 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 Function Asks a question to o o Check a condition Compute a value Used to get information about… o Size of objects o Relationship of one object to another What is snowwoman's height? What is distance between lunar robot and rock? Used to get other information o o Time Ask user for information Value What a function returns after it o o Type of value depends on kind of function o o Asks question Or performs a computation distance to returns a number is to the left of returns a Boolean value Number value o o o Whole number ex: 3 meters Fractional value, ex: 1.2 meters In lunar example What is distance of robot to rock? Function vs. Method Functions o o o Perform calculations Determine state of an object or system Return a value Methods perform an action 3-11 Types of Values Number o Boolean o “Slithy Toves?” Object o true, false String o As in example snowman, helicopter Position in world o (0, 0, 0) – center of an Alice world World Level Primitive Functions List of world level functions categories o o o o o o o o o Boolean logic Math Random String Ask user Mouse Time Advanced math Other 3-13 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 Adding Question for Number Adding Question for Number 3-16 Class Primitive Object Functions Objects have different primitive functions from the world level Class level functions o o o o o Proximity Size Spatial relation Point of view Other Object primitive proximity functions Using Function distance to Built-in distance to function o o Determines distance from center of one object to center of another object Better than guessing distance to object An Alice function is not a "stand-alone" instruction o It is nested within another instruction Checkpoint 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 Homework Read chapter 3 Do tutorials o o o o Tutorial Tutorial Tutorial Tutorial 3-1 Creating and using a variable, p 116 3-2, Creating a set instruction, p. 118 3-3, Calling an ask user function, p. 122 3-4, Using a proximity function, p. 127 Due one week after assigned with 1 week grace