18.10.2014 Views

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

function SQUARE.ROOT given NUMBER<br />

let SQRT = NUMBER/2.0<br />

let DELTA = SQRT<br />

until DELTA < 0.00001 * SQRT)<br />

do<br />

let SQRT = (SQRT + NUMBER/SQRT)/2.0<br />

let DELTA = ABSOLUTE(NUMBER/SQRT - SQRT)<br />

loop<br />

return with SQRT<br />

end<br />

By incorporating the function reference directly in the logical control phrase and eliminating the<br />

evaluation of the intermediate result, DELTA, this example reduces to:<br />

function SQUARE.ROOT given NUMBER<br />

let SQRT = NUMBER/2.0<br />

until ABSOLUTE(NUMBER/SQRT - SQRT) < (0.00001 * SQRT)<br />

let SQRT = (SQRT + NUMBER/SQRT)/2.0<br />

return WITH SQRT<br />

end<br />

Function evaluation takes the same precedence as subscripted variable evaluation. The function is<br />

evaluated prior to evaluation of the expression in which the function appears. Obviously, expressions<br />

appearing as arguments to the function are independently evaluated before the function evaluation.<br />

2.14 Global and Local Variables, Routines, Functions, and Side Effects<br />

It has been suggested that communication of values between routines is best done through an explicit<br />

argument list. The interactions between a number of routines using many global variables can<br />

be hard to follow, and a single error may have widespread repercussions. The use of explicit arguments<br />

helps to logically separate the task of a single routine within the entire program. When global<br />

variables are used in routines that interact, care must be exercised to prevent unwanted side effects.<br />

Most rigorously, any change in the value of any nonlocal variable may be termed a side effect.<br />

More commonly, the term refers to an unexpected or unforeseen consequence of any statement,<br />

usually involving a routine or function call. The practice of explicitly declaring all local variables<br />

to a routine helps avoid the inadvertent modification of any global variables.<br />

2.15 Library Functions<br />

Some functions, such as the square root and absolute value, are used so frequently that they are incorporated<br />

in the <strong>SIMSCRIPT</strong> <strong>II.5</strong> language. A list of these functions appears in Appendix B. To<br />

help distinguish the use of these functions, the names are formed from mnemonic abbreviations suffixed<br />

with the two characters .f. For example, abs.f returns the absolute value of the given argument,<br />

sqrt.f returns the square root, and log.e.f returns the natural logarithm. Recall that<br />

64

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

Saved successfully!

Ooh no, something went wrong!