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 />

Factorial(n) = n * (n-1) * (n-2) * ... * (2) * (1)<br />

This recursive function routine may be used to compute the factorial of its given argument:<br />

routine FACTORIAL(N)<br />

if N eq 1<br />

return with 1<br />

otherwise<br />

return with N * FACTORIAL(N-1)<br />

end<br />

The routine calls on itself repeatedly until it has reduced its argument to 1. If this function were<br />

called with N = 4, the factorial would be evaluated in the following steps:<br />

FACTORIAL(4) = 4 * FACTORIAL(3)<br />

= 4 * ( 3 * FACTORIAL(2) )<br />

= 4 * ( 3 * ( 2 * FACTORIAL(1) ) )<br />

= 4 * ( 3 * ( 2 * ( 1 ) ) )<br />

= 24<br />

This may not be an efficient way to compute a factorial, but it does illustrate the concept of a recursive<br />

call.<br />

An important consequence of recursion is that local variables are unique to each routine call. Each<br />

call has a separate "memory" that shares nothing with previous calls except their common routine<br />

structure. Recall that local variables are reinitialized at every entry to a routine. Global variables<br />

are defined across all levels of recursion, as their names represent the same values at all points in a<br />

program. Using global variables and passing values in argument lists are two ways that separate<br />

invocations of a recursive routine can communicate across different levels of recursion.<br />

Program efficiency and inter-routine communication are two reasons why it might be desired to<br />

have some routines behave nonrecursively. The mechanism for isolating variables and making<br />

them local, not just to a routine but to each call of a routine, involves some computational overhead.<br />

Isolating local variables of routines between routine calls also makes it impossible for a routine to<br />

transmit information from one call to another through a local variable, or to "remember" values<br />

across successive calls. In recursive routines, this can only be accomplished by using global variables<br />

or explicitly passing values as arguments.<br />

All the local variables of a program, or selected local variables in individual routines, can be defined<br />

as saved or recursive. If a variable is saved, it is stored in a memory location associated with<br />

a routine it is local to, but accessible by all references to it from any invocation of this routine. Unlike<br />

a recursive variable, a saved variable is not released when control returns to a calling program,<br />

and is not reinitialized at each new call, but retains any value assigned to it when the routine<br />

was last executed. Saved variables are initialized to zero before their first use.<br />

76

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

Saved successfully!

Ooh no, something went wrong!