18.10.2014 Views

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>SIMSCRIPT</strong> <strong>II.5</strong> <strong>Programming</strong> <strong>Language</strong><br />

While the stop statement is used to terminate processing in a main routine, the return statement<br />

is used when processing in a subprogram is complete, indicating that the flow of control should return<br />

to the calling routine (note that there is no restriction to the use of the stop statement in a subprogram<br />

for abruptly halting all further execution within the program). Some of the examples may<br />

show a comment that includes the routine name following the end statement, which is done to<br />

make clear the separation between routines.<br />

Routines used within a program are generally required to interact with program variables or data.<br />

The requirement to obtain the square root of a number, for example, may occur more than once in<br />

a program, and is also a logical subdivision of the program activity. If the statements to evaluate<br />

the square root of a number are to be grouped as a subprogram, the value of the number must somehow<br />

be transmitted to the subprogram, and the value of the square root must be communicated back<br />

to the calling routine. Values are passed from calling to called routines and back again in two ways:<br />

implicitly, as values of global variables, and explicitly, through arguments in an argument list.<br />

2.11 Global and Local Variables<br />

A global variable is a variable whose name has a common meaning throughout a program. Every<br />

use of the name of a global variable references the same data value, regardless of the routine in<br />

which the reference is made. A local variable, on the other hand, has a value defined only within a<br />

particular routine. A variable local to one routine cannot be directly referenced in any other routine.<br />

Consequently, if the same name is used for a local variable in more than one routine, the name used<br />

refers to a different value in each routine, as if a different variable name were being used in each<br />

place. Thus, local variable names that are not intended to reference the same variables may, for<br />

mnemonic reasons or even inadvertently, appear the same in different routines. In general, local<br />

variables do not maintain a permanent existence in computer memory, but rather pass in and out of<br />

existence as control passes to or from the routine to which they have been declared as local.<br />

The preamble is used to define global variables. A variable is only defined as global when it appears<br />

in a statement of the preamble. Therefore, variable names that are desired to be globally known<br />

must appear in define statements, even though their properties may be fully described by the existing<br />

background conditions. Conversely, any variable not named in a program's preamble is local<br />

to those routines where it is used. A variable may be explicitly defined as local to a routine by specifying<br />

the name in a define statement within the routine. If it is not defined as a global name, nor<br />

explicitly defined within the routine, it is implicitly defined as local by its use within a routine.<br />

It is commonly considered good practice, however, to explicitly define all local variables used by<br />

every routine. When a name is locally defined within a routine, it is unique to that routine and does<br />

not conflict with any other uses of the same name. Thus, it is possible to have many different uses<br />

of the same name — both variables and labels — in an entire program. Names declared as global<br />

can be temporarily redefined as local within a particular routine by declaring their names in local<br />

define statements within the routines. Local variables have the properties of the background<br />

56

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!