1 / 33

Variables and Functions

Variables and Functions. Chapter 3. Variables. Named storage location in computer’s memory Programs may need to store data when running Stored in a variable Types of data that can be stored Local variables Parameter variables Class-level variables World-level variables. Variable Types.

damien
Download Presentation

Variables and Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variables and Functions Chapter 3

  2. Variables • Named storage location in computer’s memory • Programs may need to store data when running • Stored in a variable • Types of data that can be stored • Local variables • Parameter variables • Class-level variables • World-level variables

  3. Variable Types • Local variables • Belongs to a specific method • Only used in that method • Variables cease to exist when method stops • Parameter variables • Holds argument that is passed to a called method • Class-level variables • Variable that belong to a specific object • World-level variables • Variable that belongs to the world

  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 • Name • Type • Initial value name type value

  5. 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

  6. variablearea Variables • Tile appearance shows the type of variable • Numbers, Boolean, Objects, String • Variable declaration - creating a variable

  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 • Use Set instructions • Done by dragging variable tile into Methods Editor

  8. 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?

  9. Function • Asks a question • Checks a condition • Computes a value • Used to get information about… • Properties of objects • Is snowwoman's face red? • Relationship of one object to another • What is distance between lunar robot and rock? • A current condition • What keyboard key was pressed?

  10. World Level Primitive Functions • List of functions categories • Boolean logic • Math • Random • String • Ask user • Mouse • Time • Advanced math • Other

  11. Value • What a function returns after it • Asks question • Performs computation • Type of value depends on kind of function • In lunar example • What is distance of robot to rock? • Number value is expected to be returned • Whole number ex: 3 meters • Fractional value, ex: 1.2 meters

  12. Types of Values • Number • As in example • Boolean • true,false • String • “Slithy Toves” • Object • snowman, helicopter • Position in world • (0, 0, 0) – center of an Alice world

  13. Asking User for Input • World has three primitive functions that ask for user input • Each function is displayed in a dialog box Is it Yes orNo?

  14. Adding Question for Number

  15. Class Primitive Object Functions • Objects have different primitive functions from the world • Proximity • Size • Spatial relation • Point of view • Other Object primitive proximity functions

  16. Using Function distance to • Built-in distance to function • 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 • It is nested within another instruction

  17. 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?

  18. Creating Math Expressions • Math Operators are used to create math expressions • Math expressions perform a calculation and return a value • Simple math expression • 12 + 2 • Value returned is 14 • Operands appear to the right and left of an operator • Example with variables “payRate” and “hoursWorked” • payRate * hoursWorked

  19. 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? • Can use • pointOfView property • Primitive function

  20. Avoiding Collisions • When program is run, robot collides with rock • Distance between two objects is measured center-to-center • One way to avoid a collision • Reduce calculated distance to object • Use expression to subtract small number (1 or 2) from distance

  21. Expressions in Arguments • Math expressions can be used in place of • Fixed value • To adjust value returned by function Click down arrow to get math operation • Adjusts distance to move to not collide with rock

  22. Adding Math Expression in Alice

  23. Subtracting Functions • Two meters subtracted from distance is arbitrary • To be more precise, subtract width of rock • But still not accurate • Resulting expression subtracts value of one function from value of another function

  24. More Precision • The most accurate way to determine distance and to avoid collision • Add half width of both objects subtract from distance • Be careful of excessive parentheses

  25. Checkpoint • What do the following operators do in most programming languages? • + - * / • What does a math expression do? • When you use the move to instruction to move one object to another object, why do they collide? • 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?

  26. Working with Strings and Text • A string is a sequence of characters • Strings represent • Names • Addresses • Messages • etc.

  27. 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 Defines string variable called name Asks user for name Stores in variable name Puts contents of name variable in bubble

  28. Joining Strings • String Concatenation • Joins string a with string b • Strings a and b are arguments • Does not change the valuesof strings a or b • World function • a joined with b • Drag a joined with b from string section in World function to where you want to join strings

  29. Example of a joined with b Hello joined with was only thing added

  30. 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

  31. Example of what as a string

  32. 3D Text • 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

  33. Homework • Read chapter 3 • Do tutorials • Tutorial 3-1 Creating and using a variable, p 116 • Tutorial 3-2, Creating a set instruction, p. 118 • Tutorial 3-3, Calling an ask user function, p. 122 • Tutorial 3-4, Using a proximity function, p. 127 • Tutorial 3-5 Using math to avoid collisions, p 132 • 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

More Related