Lecture 5 Flashcards
What is the scope of a variable?
The scope of a variable is the range of statements in which the variable is visible.
When is a variable visible in a statement?
A variable is visible if it can be referenced in that statement.
What are the types of variables based on scope?
Local and non-local variables.
What is static scoping?
A method of linking names to non-local variables so that the variable’s scope can be determined before the program runs.
Which languages support nested static scopes?
Ada
JavaScript
Common LISP
Scheme
Fortran 2003+
F
Python.
Which languages do not support nested static scopes?
C-based languages.
How is a reference to a variable found in a static-scoped language?
The search starts in the subprogram where the variable is referenced. If not found, it continues in the subprogram’s static parent.
Can hidden variables be accessed in some languages?
Yes, for example, in Ada.
What is a hidden variable in static scope?
A variable in an outer scope that is hidden by another variable with the same name in an inner scope.
How are variables typically defined in blocks?
As static dynamic variables.
What are blocks in programming?
Sections of code with their own local variables whose scope is minimized.
Which languages do not allow block scope inside functions?
Java and C#
Where must data declarations appear in C89?
At the beginning of the function, except in nested blocks.
Which languages allow variable declarations anywhere in the function?
C99, C++, Java, JavaScript, C#.
What is a global variable?
A variable defined outside all functions, potentially visible to all functions in the program.
How are global variables managed in C and C++?
Through declarations and definitions, with memory allocation only occurring at the definition.
What is a drawback of static scoping?
It often allows more access to variables and subprograms than necessary, and software is highly dynamic.
What is a merit of dynamic scoping?
Parameters passed from one subprogram to another can be variables defined in the caller, and none of these needs to be explicitly passed.
What is the referencing environment of a statement?
The collection of all variables visible to the statement.
How is the referencing environment determined in a static scoped language?
By the local scope plus all ancestor scopes.
How is the referencing environment determined in a dynamic scoped language?
By the local variables plus variables of all currently active subprograms.
What is a named constant?
A variable that is bound to a value only once.
Why are named constants useful?
They aid readability and program reliability.
Give an example of a named constant in Java.
final int len = 100;