And now, at the risk of being accused to be impossible to understand, I'll try to outline a technique of which Pascal users are aware, but one that (because of large memory overheads) should probably be avoided on the Oric: recursion. To describe recursion, I'll compare it to iteration by using a simple example (which will probably only complicate things, but here goes anyway).
How do we define the familiar power 1 function? There are two obvious ways to do it. One is to say that xy is 1 if y=0, else xy is x*x*...*x, y times. Here's a C function to do this:
unsigned int power (unsigned int x, unsigned int y) { unsigned int result; if (y == 0) return 1; for (result = x; y > 1; y--) result *= x; return result; }
This is the iterative way of doing things: in short, use a loop. It's nice, it's fast, it takes minimal amounts of memory (well, it does in this case). However, we may also define power as follows 2: x0 = 1, and xy = x * xy-1. Think about it. What this says is that, if you have the (y-1)-th power of x, you can always find the y-th power by multiplying the whole thing times x. It makes sense, doesn't it? In mathematics, this is called a recurrence relation: defining something in terms of itself. In programming, it's called recursion 3. Here's a function to do just that:
unsigned int power (unsigned int x, unsigned int y) { if (y == 0) return 1; else return x * power (x, y - 1); }
This is all! As you can see, you may call a C function from within itself. The recursive version of power does the following: if y==0, the result is 1. Otherwise, the result is x times xy-1. Let's see a trace of the function call power(2,3):
2 * power (2,2) 2 * (2 * power (2, 1)) 2 * (2 * (2 * power (2, 0))) 2 * (2 * (2 * 1)) 2 * (2 * 2) 2 * 4 8
The recursive power is a good-looking function: it adheres to the mathematical model for raising to a power, and it's much simpler than the iterative one. However, always remember that an amount of memory is allocated on the C stack for every function call. This, coupled with the slightly lower speed of recursion, makes the whole thing rather undesirable for a small 8-bit machine. Take this with a pinch of salt, though. There are always cases where you might want to use recursion.
In case you feel like using it, though, here is the most important tip on recursion: never forget that there are two cases in a recursive function. The base case, which does not recurse any more (i.e. x0 in our example), and the recursing case (or cases). In order for the recursion to end (we usually want this to happen), the base case must always be reached, sooner or later.
- 1. No, C (like Pascal) does not have a built-in power operator. Probably so people like me can use raising to a power as an example of recursion.
- 2. To all you mathematics people: for simplicity, I'll only deal with positive integer powers of integers. For the same reason, I don't check for the most dreaded undefined case 00.
- 3. In most other sciences, it's called ‘nonsense’.




Add new comment