Names , Bindings and Scopes ! (session 3)

Session 3: Names, Bindings, and Scopes


A. Variables

A variable is an abstraction of a memory cell. The lifetime of a variable is the time during which it is bound to a particular memory cell.

Avariable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in programming languages.

Example : int angka               Variable name



Variables can be characterized as a sextuple of attributes:

– Name                        – Address        – Value                        – Type              – Lifetime        – Scope


Categories of Variables by Lifetimes

  1. Static–bound to memory cells before execution begins and remains bound to the same memory cell throughout execution, e.g., C and C++ static variables in functions.

Advantages     : Efficiency (direct addressing), history-sensitive subprogram support

Disadvantage  : Lack of flexibility (no recursion)

  1. Stack-dynamic–Storage bindings are created for variables when their declaration statements are elaborated.If scalar, all attributes except address are statically bound, e.g. local variables in C subprograms (not declared static) and Java methods.

Advantage        : Allows recursion, conserves storage

Disadvantages  : Overhead of allocation and deallocation, subprograms cannot be

history sensitive, inefficient references (indirect addressing)

  1. Explicit heap-dynamic –– Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution. Referenced only through pointers or references, e.g. dynamic objects in C++ (via new and delete), all objects in Java.

Advantage      : provides for dynamic storage management

Disadvantage  : inefficient and unreliable

  1. Implicit heap-dynamic--Allocation and deallocation caused by assignment statements, e.g. all variables in APL; all strings and arrays in Perl, JavaScript, and PHP

Advantage: flexibility (generic code)

Disadvantages: Inefficient, because all attributes are dynamic; loss of error detection





B. Names

Name is a string of characters used to identify some entity in a program.that is unique, means that there is only one existence of the name, and different each other. Variable namesare the most common names in programs. Names are often referred as identifiers.

  1. Length
  • If too short, they cannot be connotative
  • In C++ no limit, but implementers often impose one
  1. Special Character
  • In C++, name can not start with any symbol or number, thus only able to use alphabet or “_” (underscore) symbol as the first word, and no “ “ (space) permitted, so instead of using space, underscore can be used as the exchange.
  1. Case Sensitivity

Disadvantage: readability (names that look alike are different), e.g. “A” is different with “a”, etc.

  • Names in the C-based languages are case sensitive

C. Special Words

Special words in programming languages are used to make programs more readable by naming actions to be performed.  They also are used to separate the syntactic entities of programs.

Keyword is a word of programming languages that is special only in certain context. This can still be used as an identifier or variable name, e.g. final, override, etc.

Reserved word is a special word of a programming language that cannot be used as a user-defined name, e.g. for, int, if, etc.

D. Bindings

Abindingis an association between an entity and an attribute, such as between a variable and its type or value, or between an operation and a symbol. Binding time is the time at which a binding takes place.

Binding Time :

  • Language design time — bind operator symbols to operations
  • Language implementation time– bind floating point type to a representation
  • Compile time — bind a variable to a type in C or Java
  • Load time — bind a C or C++ static variable to a memory cell)
  • Runtime — bind a nonstatic local variable to a memory cell

Storage Bindings

  • Allocation – getting a cell from some pool of available cells
  • Deallocation – putting a cell back into the pool




E. Scopes

The scope of a variable is the range of statements over which it is visible. The local variables of a program unit are those that are declared in that unit. Local variables are only known in the unit and can not be used in different unit. The nonlocal variables of a program unit are those that are visible in the unit but not declared there. Global variables are a special category of nonlocal variables, where it is universally known by any unit and usually declared first before any scope or unit. The scope rules of a language determine how references to names are associated with variables.

Example :

#include <stdio.h>

int JumlahSiswa;                                       Global Variable; known by the NamaSiswa and main unit. It is not visible in those 2 units, but this global variable is usable in those 2 units

void NamaSiswa()
{ // Scope start from here

char Nama[30];                       Local Variable; only known and usable in this unit{


} //Scope ends here


int main()
{ // Scope start from here

int NilaiSiswa;             Local Variable; only known and usable in this unit

} //Scope ends here

  1. Static Scope
  • Based on program text
  • To connect a name reference to a variable, you (or the compiler) must find the declaration
  • Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name
  • Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent
  • Some languages allow nested subprogram definitions, which create nested static scopes (e.g., Ada, JavaScript, Common LISP, Scheme, Fortran 2003+, F#, and Python)
  • Works well in many situations
  • Problems:In most cases, too much access is possible; as a program evolves, the initial structure is destroyed and local variables often become global, subprograms also gravitate toward become global, rather than nested


  1. Dynamic Scope
  • Based on calling sequences of program units, not their textual layout (temporal versus spatial)
  • References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point

Example :


function big()                                                     Static scoping

{                                                                          Reference to x in sub2 is to big’s x

function sub1()

var x = 7;

functionsub2()                                              Dynamic Scoping

{                                                                    Reference to x in sub2 is to sub1‘s x

var y = x;


var x = 3;



  1. Global Scopes

C, C++, PHP, and Python support a program structure that consists of a sequence of function definitions in a file. These languages allow variable declarations to appear outside function definitions.

C and C++have both declarations (just attributes) and definitions (attributes and storage). A declaration outside a function definition specifies that it is defined in another file.











Source(s) :

Slide Binus Maya

Comments are closed.