ZBasic Language Reference
80
ZBasic Microcontrollers
3.16 Array Data Order
RAM-based arrays are stored sequentially in memory with the first index varying the fastest and the last
index varying the slowest. Consider a two-dimensional array defined as:
Dim ba(1 To 3, 1 To 2) as Byte
The bytes of this array are assigned addresses in memory sequentially as:
ba(1,1) ba(2,1) ba(3,1) ba(1,2) ba(2,2) ba(3,2)
Whether this constitutes row-major order or column-major depends on whether you consider the first
index of a two-dimensional array to be the column and the second index the row or vice versa. To a large
extent, this is a non-issue as long as you remember that the first index varies the fastest.
When thinking about Program Memory data tables, on the other hand, the perception does matter
because the initializer data is arranged in rows and columns and you need to know how to get the array
element that you want. To do so, always specify the column index first and the row index second. This
order was adopted to maintain compatibility with the BasicX compiler.
3.17 Recursion in Subroutines and Functions
A subroutine or function may be invoked recursively. This means that among the statements in the
routine there is one or more that invokes the same subroutine or function again, either directly or
indirectly. Clearly, the recursive invocation must be conditional so that at some point the recursion
ceases.
In Section 2.3.2, Defining Functions, an example function was given for computing factorial. Here is the
same function written using recursion.
Function Factorial(ByVal val as Integer) As Integer
If (val > 1) Then
Factorial = Factorial(val - 1) * val
Else
Factorial = 1
End If
End Function
Although it may look confusing at first, the logic is fairly simple. The idea is based on the fact that the
value of N factorial is equal to N times the factorial of (N-1). That logic is directly expressed in the second
line of code. Note that the identifier Factorial is used in two distinctly different ways in the second line
of the function. On the left side of the equal sign, the identifier Factorial refers to the return value
variable while that on the right side of the equal sign is a recursive invocation of the Factorial function
itself. The compiler is able to distinguish the two uses by the presence of parentheses following the
name. Since the return value variable can never be an array, the two types of references are easily
distinguishable.
The negative aspect of recursion is that it can consume a large amount of stack space. Each time a
function or subroutine invocation is performed, the processor allocates additional stack space. The extra
space is used to hold some tracking information to allow the processor to return to executing the code
that immediately follows the invocation. Also, stack space is required for any parameters that are passed
to the subroutine/function and for any variables that are defined inside the subroutine/function. Lastly, in
the case of a function only, stack space is required to hold the return value from the function. Clearly,
Factorial(10000) would require more stack space than is available even if all the RAM were
dedicated to the stack.
|