new mexico computer science for all variables and scope maureen psaila-dombrowski

13
New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski

Upload: beryl-heath

Post on 04-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski

New Mexico Computer Science For All

Variables and Scope

Maureen Psaila-Dombrowski

Page 2: 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

Page 3: New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski

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

Page 4: New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski

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

Page 5: New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski

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

Page 6: New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski

•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

Page 7: New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski

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

Page 8: New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski

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

Page 9: New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski

•Show program with different types of variables

Page 10: New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski

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]

Page 11: New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski

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

Page 12: New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski

•SIMPLE NETLOGO PROGRAM WITH SLIDER INPUT BOX, SWITCH AND CHOOSER

Page 13: New Mexico Computer Science For All Variables and Scope Maureen Psaila-Dombrowski

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