HARSFEN0602ElmoHARmonicaSoftwareManual

PRELIMINARYDRAFT

Example:

 

 

 

 

 

 

 

function factorial() ;

**Function prototype

 

 

 

 

 

IA[1]=3

 

 

 

IA[2]=1

 

 

 

factorial()

**Function call

 

 

BG

 

 

 

 

 

 

function factorial()

**Function for factorial

 

 

global int IA[] ;

**Definition of 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 of the 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

 

 

 

 

the return which was on

 

 

 

 

top the call stack)

 

return

Unchanged

Unchanged

Empty (Program jumps to

 

 

 

 

the BG which was on top

 

 

 

 

the call stack)

 

BG

Unchanged

Unchanged

Unchanged

 

 

 

 

 

46

Page 48
Image 48
Elmo HARmonica, HARSFEN0602 software manual Return