1 / 12

New Mexico Computer Science For All

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.

angelo
Download Presentation

New Mexico Computer Science For All

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. New Mexico Computer Science For All Local Variables in Netlogo Maureen Psaila-Dombrowski

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

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

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

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

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

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

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

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

  10. NetLogo Example: Distance from (0,0)

  11. 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)

  12. Further Information • If you would like more information: • NetLogo Manual • NetLogo Dictionary

More Related