5

How is COBOL's scope defined? Is it statically scoped?

NexAddo
  • 752
  • 1
  • 18
  • 37

3 Answers3

4

Cobol has compile time binding for variables, sometimes called static scope.

Within that, Cobol supports several layers of scope within programs:

  • "External" variables are the equivilent of a Fortran or assembler common section, they are truly global.

  • "Global Program Scope" variables declared in working storage as global are visible to the entire program in which they are declared AND in all nested subprograms contained in that program.

  • "Program Scope" variables declared in working storage are visible to the entire program in which they are declared.

  • "Program Scope" variables declared in local storage are visible to the entire program in which they are declared, but are deleted and reinitialized on every invocation. Think thread scoped, sorta.

  • "Nested Program Scope" Cobol does not distinguish between programs and functions/procedures, its equvilent of a procedure or function is called a program. An infinite number of programs can be contained within a program, and the variables of each are visible only within the scope of that individual program. You could think of this as function/procedure scope.

The OO extensions that many vendors have, and the 2002 standard, defines the traditional public/protected/private object scope and method scope.

"Cobol" is as old as Radar, Laser, and Scuba, can we please stop acronymizing it?

Joe Zitzelberger
  • 4,238
  • 2
  • 28
  • 42
1

All variables in a COBOL program are globally scoped. In fact, there are no "scopes" (in traditional COBOL, I'm not messing with OO extensions), but just "modules" or "programs".

Intermodule communication is done via the Linkage Section (usually passed by ref), and also all variables there are visible from the called module.

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
0

COBOL uses static (lexical) scope (as do C, C++, Java and Pascal). Dynamic scope is not all that common in the programming world. I think some versions of Lisp and SNOBOL used dynamic scope.

If you are interested in understanding scope with respect to programming languages you should review this document

NealB
  • 16,670
  • 2
  • 39
  • 60
  • Ruby is also a dynamically scoped language. – NexAddo Feb 25 '11 at 02:32
  • No, Ruby is lexically scoped, not dynamically scoped. Nobody would want to use it if it were dynamically scoped. BTW, probably the most-used dynamically scoped languages today are Emacs Lisp and MUMPS. Perl and Common Lisp support optional dynamic scoping. – Nate C-K Sep 14 '12 at 03:32