Variables – local, global, and parameter

Local

A local variable is only available to the module (or block of code) it is within.  That is its “scope”.

FUNCTION addThis(a, b)
total ← a + b
OUTPUT (total)
End addThis

FUNCTION Main
firstNum ← 5
secondNum ← 6
Call addThis(firstNum, secondNum)
End Main

The variable total is local to the module addThis, and cannot be seen by the Main module.

Another example is a For loop

For i ← 1 to 5
    OUTPUT (i)
End For

The variable i is local to the for loop and doesn’t exist outside it.

Global

total ← 0

FUNCTION addThis(a, b)
total ← a + b
End addThis

FUNCTION Main
firstNum ← 5
secondNum ← 6
Call addThis(firstNum, secondNum)
OUTPUT total
End Main

Because the variable total has been created outside the Main and addThis module, it is now known as a global variable and is available to all modules.

Parameter

When passing variables between modules we use the term passing parameters.

FUNCTION addThis(a, b)
total ← a + b
OUTPUT (total)
End addThis

FUNCTION Main
firstNum ← 5
secondNum ← 6
Call addThis(firstNum, secondNum)
End Main

In the above example the firstNum, secondNum, a, and b are parameters.  firstNum is being passed from Main to a in addThis().  The firstNum and secondNum are known as Actual parameters, they are the variables being passed to a module.  The a and b in addThis() are the Formal parameters as they are received.

There are two ways to pass parameters, by value and by reference.  Passing a parameter by value means making a copy in a new memory location for the receiving module.  Passing a parameter by reference is passing the memory location (the address) of the original variable.  What this means is if the variable passed by reference is changed the data in the original variable will change.   Let’s look at two examples.  The first one is where the parameter is passed by value, the second it is passed by reference.

Passed by value

FUNCTION changeName(name)
name ← "Bob"
OUTPUT (name)
End changeName

FUNCTION Main
firstName ← "Dave"
Call changeName(firstName)
OUTPUT (firstName)
End Main

Result:
> Bob
> Dave

Passed by reference

FUNCTION changeName(name)
name ← "Bob"
OUTPUT (name)
End changeName

FUNCTION Main
firstName ← "Dave"
Call changeName(firstName)
OUTPUT (firstName)
End Main

Result:
> Bob
> Bob

The variable name in the module changeName changes what is in the memory location of firstName.  They are still both local variables in their modules; name is not accessible by the Main module, and firstName is not accessible by the changeName module.

Actual and Formal parameters

Actual parameters are parameters in the calling module, formal parameters are the parameters in the receiving module.  In the code below “average” is the actual parameter and “a” is the formal parameter.

FUNCTION calcAverage(a)
INPUT(totalItems)
INPUT(totalPrice)
a ← totalPrice/totalItems
End calcAverage

FUNCTION Main
Call calcAverage(average)
OUTPUT (average)
End Main

Lifetime of variables

The term “lifetime” relates to how long and where a variable remains in existence.  For example once a global variable is created it remains available (whether it has anything in it or not) for the life of the program.

Scope of variables

The term “scope” refers to where variables are available to pieces of code.  The scope of a global variable in the whole program.  The scope of a local variable is within the block of code.

Below the variable “i” scope and lifetime is only within the fixed iteration whereas “w” scope and lifetime is the whole of Main module and during the time Main is running.

FUNCTION Main
w ← 0
FOR i ← 0 to 10
OUTPUT(i)
END FOR
End Main

If we tried to use “i” after the iteration had finished it would create a run time error.