new mexico computer science for all local variables in netlogo maureen psaila-dombrowski
TRANSCRIPT
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
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
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
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
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
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
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
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
NetLogo Example: Distance from (0,0)
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)
Further Information
•If you would like more information:
▫NetLogo Manual
▫NetLogo Dictionary