CS 211 Lesson 14
Variable Scope and Lifetime
Quote:
Character isnt inherited. One builds it daily by how one thinks and acts Helen Gahagan Douglas
Lesson Objectives:
Understand the difference between local and global variables
Understand the concepts of variable scope and variable lifetime
Be able to define and use global variables
Be able to define and use persistent variables
Lesson:
I. MATLAB Concepts
A. Variable Scope
Variable scope refers to the extent of code in which a variable can be referenced (accessed and modified).
Variables defined inside a function are called local variables.
The scope of local variables and dummy arguments is limited to the function in which they are defined. If the function contains nested functions, the code in the nested functions can access all variables defined in their "parent" function.
Variables stored in the MATLAB workspace (called global memory) are called global variables.
By default, the scope of global variables is the command line and all scripts.
Most of the time we want the protection (encapsulation) provided by local variables defined inside functions.
Most of the time using global variables is a bad idea.
Occasionally, it is useful to access global variables from within a function when the variables contain large amounts of data. There can be significant memory and time savings if the data is large.
B. How to use Global Variables
To access a global variable from within a function, you must explicitly label the variable as global.
The following code provides an example of using a global variable (Big_data).
function example_use_of_global()
global Big_data % this is the important line
[Rows, Cols] = size(Big_data);
for j in 1:Rows
for k in 1:Cols
if Big_data(j,k) > 100
Big_data(j,k) = 0;
end
end
end
end % function example_use_of_global
A global variable must be declared global before it is used the first time in a function.
Global variable declarations should be placed at the beginning of a function definition.
For a demonstration of how using a global variable can improve performance, see global_demo.m
C. Variable Lifetime
Variable lifetime refers to how long a variable remains in existence.
The lifetime of a global variable is indefinite -- the variable exists until it is explicitly cleared (with the clear global command) or until MATLAB terminates.
The lifetime of a local variable is limited to the execution time of the function in which the variable is defined. When a function terminates, its local workspace ceases to exist (which includes all local variables and dummy argument variables).
If a function gets called more than once, its local variables are "recreated" each time it is called.
The use of persistent variables extends the lifetime of local variables across multiple function calls.
To make a local variable persistent, you must explicitly label the variable as persistent
The following code provides an example of a function that uses a persistent variable. You can download Persistent_Demo.m and run the code. Compare this to Local_Variable_Demo.m.
function Persistent_Demo()
persistent Counter % this is the important line
if isempty(Counter)
Counter = 1; % initialize first time
else
Counter = Counter + 1; % increment other times
end
fprintf('Counter = %d\n', Counter)
end % function persistent_demo
While a persistent variable has an extended lifetime, it does not have an extended scope.
II. Good Programming Practices
Only use global variables when you have large matrices and
passing the large matrices as arguments requires too much time or too much
memory.
Limit the scope of variables to the smallest practical scope
-- it will save you time and energy when you are debugging.
III. Algorithms
(None for this lesson)
Lab Work: Lab 14
References: Chapman Textbook: sections 5.4-5.5