3.1.5 Scope
Table of Contents
1 The Scope of variables, constants, functions and procedures
Learn It
- The scope of a variable is the range of code that it applies to.
- You can think of scope as applying to the visibility of a variable.
- A variable is in scope if it can be seen by a particular line of code.
Learn It - Block Scope
- A variable with block scope is only valid within a particular block of code such as a for loop.
- Look at the example below.
OUTPUT "Here are the even numbers up to 100" FOR i <-- 1 TO 100 IF i MOD 2 = 0 OUTPUT i ENDIF ENDFOR OUTPUT "Finished"
- The variable
i
has scope between lines 2 and 6. - If a programmer was to try and use the variable before line 2 or after line 6, there would be a run-time error.
Learn It - Function/Procedure scope
- A variable with function scope is only valid within a particular function.
- Look at the example below.
FUNCTION square(n) RETURN n * n ENDFUNCTION FUNCTION sumSquares(n) total <- 0 i <- 0 WHILE i <= n: total <- total + square(i) i <- i + 1 ENDWHILE RETURN total ENDFUNCTION
- Here we have two functions, both using a variable called n.
- Because the two variables both have function scope, they are completely unrelated.
- There is no risk of name collision when using the identifier
n
in either of the two functions. - Similarly, the second function contains variable identifiers
i
andtotal
. As these both have scope only within thesumSquares
function, the identifiers can be used elsewhere in the program, with no risk of altering the expected return of the function.
Learn It - Module Scope
- Variables have module scope if they can be accessed by any line in a file or once a module has been imported.
- This is tricky to show in Pseudocode, so we'll switch to Python 3.
- Image we have a file called foo.py
bar = 'Hello' def outputModuleVar: print(bar)
- The variable
bar
can be viewed (but not necessarily altered) by any part of the the file foo.py - We could also create a second file called baz.py
import foo print(foo.bar)
- Because the variable
bar
has module scope, it can be accessed by any file that imports foo.py
Learn It - Global Scope
- A variable has global scope if it can be accessed from anywhere in a project.
- Variables with global scope are usually considered bad practice in most languages, due to the increased risk of name collisions.
Document It
- In your chosen programming language, write a script that uses block, function and module scope for it's variables.
- Take a screenshot of your code, and then annotate it in a graphics editor, identifying which variables have which scope.
Try It
- In pseudocode, write two small scripts, that use block, function and module scope.
- Give the code to a peer, and ask them to annotate and identify the scope of the variables.