new mexico computer science for all local variables in netlogo maureen psaila-dombrowski

12
New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

Upload: rhoda-warner

Post on 17-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

New Mexico Computer Science For All

Local Variables in Netlogo

Maureen Psaila-Dombrowski

Page 2: New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

Variables in Computer Science

•A variable is a container that holds a value

•It has the following fixed properties A name, used in the program to access

the value. A type (number, text, boolean, etc.) A physical location in memory

•The value stored in the variable can be used and changed as the program is executed

Page 3: New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

Steps for Using Variables

•Three Steps for Using Variables

▫Declare – allocates the space for the variable and sets the name given to the variable

▫Initialize – set the initial value of the variable

▫Call/Modify – Use it in the program or change the value stored in the variable as the program is executed

Page 4: New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

Local Variables

•A local variable is a variable that can only be used in the procedure or command block where it is declared.

•Cannot be accessed by the program in other locations

Page 5: New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

Local Variables in NetLogo

•Declare and initialize local variables using the let command

example: let newvar 15

▫Declares a variable named newvar and initializes to 15

Page 6: New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

Local Variables in NetLogo

•Call a local variable

example: set color newvar

▫Sets the color of the turtle to color 15 (red)

•Modify a local variable using the set command

example: set newvar 25

▫Modifies the value of newvar to 25

Page 7: New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

NetLogo Example: Distance from (0,0)

•Setup Procedure▫Clears the NetLogo world, creates 50 turtle,

scatters them and colors them red▫Set the color of turtle #1 to white

Page 8: New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

NetLogo Example: Distance from (0,0)

•Walk Procedure▫ Declares and initializes the variable “dist”▫ Moves all turtles forward 1 step▫ Modifies the value of “dist” to be the distance of

turtle #1 from the origin▫ Prints

Page 9: New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

NetLogo Example: Distance from (0,0)

•Walk Procedure▫ Declares and initializes the variable “dist”▫ Moves all turtles forward 1 step▫ Modifies the value of “dist” to be the distance of

turtle #1 from the origin▫ Prints

Page 10: New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

NetLogo Example: Distance from (0,0)

Page 11: New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

Summary•A variable is a named memory location

▫Holds a value▫Value can used and/or changed as the

program is executed

•Three steps for using variables▫Declare▫Initialize▫Call/Modify

•A Local Variable used locally in the program where it is declared (procedure or command block)

Page 12: New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

Further Information

•If you would like more information:

▫NetLogo Manual

▫NetLogo Dictionary