Scope is a method of finding out precisely which variables are accessible at which subprograms. Scope charts consist of boxes inside boxes inside boxes, with names given to the boxes, and identifiers inside the boxes showing that they can be accssed in that box and all boxes within that box.
You have procedures inside procedures, variables inside procedures, and your job is to try to figure out when each variable can be seen by the procedure.
A global variable is a variable defined in the main program. Any subprogram can see it, use it, and modify it.
All subprograms can call themselves, and can call all other subprograms defined before it.
The main point here is: within any block of code (procedure, function, whatever), the only identifiers that are visible are those defined before that block and either in or outside of that block.
Another thing:
program Stupid; var A; procedure StupidToo; var A; begin A := 10; writeln (A) end; begin (* Main *) A := 20; writeln (A); StupidToo; end. (* Main *)The output is:
20 10
If two variable with the same identifiers are declared in a subprogram and the main program, the main program sees its own, and the subprogram sees its own (not the main's). ALWAYS, the MOST LOCAL definition is used for an identifier.
Here's a scope chart:
![]() Previous lesson |
![]() Next lesson |
![]() Contents |
![]() Index |
![]() e-mail me |