ElmoMC SimplIQ Software Manual User Manual

Page 57

Advertising
background image

SimplIQ

Software Manual

4BThe

SimplIQ

User Programming Language

MAN-SIMSW (Ver. 1.4)

5-28

This code executes the sequence:
JV=1000;IA[1]=1;BG;

After executing JV=1000, the program jumps to the subroutine JustSo. Before doing so, it
stores its return address, which is the place in the code at which execution should resume
after the routine is complete. In this example, the return address is the line number of
instruction BG, which is just after the subroutine call.

The return command instructs the program to resume from the stored return address. After
execution is resumed at that address, it is no longer required and is no longer stored.

Functions may call each other; in fact they may even call themselves. Return addresses for
nested function calls are stored in a call stack, as shown in the following example:
function factorial();

Function

prototype.


IA[1]=3

IA[2]=1

factorial()

Function

call.

BG


function factorial()

Function for factorial.

global int IA[];

Define array as global inside function.

IA[2]=IA[2]*IA[1]

Recursive

algorithm.

IA[1]=IA[1]-1

if (IA[1]>1) factorial() ; end

Recursive

call.

return

Function

end

The factorial function in this example calculates the factorial of 3 in IA[2]. The variable IA[1]
counts how many times the function factorial is executed.

The program executes as follows:

Code IA[1]

IA[2]

Call

Stack

IA[1]=3

3 Undefined

Empty

IA[2]=1

Unchanged 1

Empty

factorial

Unchanged Unchanged

→BG

IA[2]=IA[2]*IA[1]

Unchanged 3

Unchanged

IA[1]=IA[1]-1

2 Unchanged

Unchanged

if (IA[1]>1)

factorial(); end

Unchanged Unchanged

→RT
→BG

IA[2]=IA[2]*IA[1]

Unchanged 6

Unchanged

IA[1]=IA[1]-1

1 Unchanged

Unchanged

if (IA[1]>1)

factorial(); end

Unchanged

Unchanged Unchanged (condition is false)

return

Unchanged Unchanged

→BG (program jumps to
return on top of call stack)

return

Unchanged Unchanged

Empty

(program jumps to BG

on top of call stack)

BG

Unchanged Unchanged

Unchanged

Advertising