New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski
Variables - review Variable: container that holds a value that can be used and changed. 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 ▫Get/Set– Use it in the program or change the value stored in the variable as the program is executed
Types of Variables There are three types of NetLogo variables Local Variables Agent Variables Global Variable Differ in scope of where they can be used Local – used in the block where declared Agent – used by specific type of agents Global – used anywhere in the program
Local Variables Already discussed at length Can only be used in the procedure or command block where it is declared Can be used by any agent How to Use: ▫Declare and initialize local variables using the let command ▫Use the local variable in an expression or ▫Modify a local variable using the set command
Agent Variables Three types of Agent Variables ▫Turtle Variables Each turtle has its own value for every turtle variable ▫Patch Variables Each patch has its own value for every patch variable ▫Link Variables Each link has its own value for every link variable
Built-In Agent Variables ▫Some agent variables are built-in to NetLogo Turtles: color, heading, xcor, ycor, etc. Patches: pcolor, pxcor, pycor, etc. Links: color, shape, etc. User-defined variables ▫Can make user defined agent variables by declaring them at the beginning of the program turtles-own [ energy ] patches-own [ grass? ] links-own [ strength ] Agent Variables
How to use an agent variable ▫Declare the agent variable if necessary ▫Change/modify the variable using the set command ▫Generally can only be used/set by the specific agent Turtles Turtle Variables Patches Patch Variable Link Link Variables
Agent Variables Exceptions ▫An agent can set a different agent's variable by using the ask command. ask patches [……. ask turtle 5 [ set color blue] ;; changes turtle 5’s color to blue …] ▫One agent can read another agent’s variable using the of command show [color] of turtle 5 ;; prints turtle 5’s color ▫A turtle can read and set patch variables of the patch it is standing on directly ask turtles [set pcolor blue] ;;makes patches under all turtles blue
Show program with different types of variables
Global Variables The is only one value of each global variable at any time You can declare a global variable by ▫Using an input device on the interface tab (switch, slider, chooser or input box) ▫In the code by using the globals keyword at the beginning of the code globals [NumTurtles]
Global Variable Can be used and modify ▫At any location in any procedure in the code ▫By any agent Use by using the variable name Modified by the using ▫In the Code with the set command set NumTurtles 100 ▫On the Interface tab with switch, slider, chooser or input box
SIMPLE NETLOGO PROGRAM WITH SLIDER INPUT BOX, SWITCH AND CHOOSER
Summary Variable: container to hold a value To use: Declare, Initialize and Use/Modify Three types: ▫Local Variables ▫can be used by any agent but only in the procedure or block it is declared in ▫Agent Variables ▫Each agent has its own value for each agent variable ▫Used only by that specific agent (exceptions) ▫Global Variables ▫Only one value at any time ▫Used by any agent in any procedure