Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Robotics with the Board of Education Shield for Arduino

Robotics with the Board of Education Shield for Arduino

Global vs Local Variables

Local Variables

So far, we’ve declared variables inside a function block—the indented portion of code that follows declaration of a function—which means they are local variables.  Only the function declaring a local variable can see or modify it.  Also, a local variable only exists while the function that declares it is using it.  After that, it gets returned to unallocated memory so that another function (like loop) could use that memory for a different local variable.

Global Variables

If your program has to give more than one function access to a variable’s value, you can use global variables.  To make a variable global, just declare it outside of any function, preferably immediately after your import statements.  Then, all functions in the program will be able to modify or retrieve its value. 

Example script: store_retrieve_global

The next example script declares a global variable called k, assigns a value to it, and then uses its value from within a function. In contrast, the variable called value is a local variable and only exists within the add_k function. If you try to display.scroll(value), you will receive an error.

  • Enter, name, save, and flash the script store_retrieve_global to your micro:bit.
#store_retrieve_global
from microbit import *

k = 5    

def add_k(value):
    value = value + k
    return value

display.scroll(add_k(3))

Example: store_change_retrieve_global

If you want to permanently change the global variable from within the function, you must use the keyword global to call upon the global variable. In the script store_change_retrieve, k is created as a global variable with the value 5 assigned. Each time k is called upon in the function add_k it gets incremented by 1.

  • Enter, name, save and flash the script store_change_retrieve_global to your micro:bit.
#store_change_retrieve_global

from microbit import *

k = 5    

def add_k(value):
    global k
    k += 1
    value = value + k
    return value

display.scroll(add_k(3))
display.scroll(add_k(3))
  • The display will first scroll 9, then 10. Do you understand why?

Printer-friendly version

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress