Page 86 - The Unofficial Guide to Lego Mindstorms Robots
P. 86
75
efficient because the code is just compiled once. With a macro, the entire macro body would be placed at each point where it
w as called.
Th e RCX imposes three crippling restrictions on subroutines. First, you can't call another subroutine from within a subroutine.
As a consequence, a subroutine also cannot call itself. Second , you can't pass parameters to a subroutine or get a return value.
Th ird, no more t han eight subroutines can be defined for a single program. These limitations are imposed by the RCX's
bytecode interpreter, which is defined in the firmware. To get around restrictions like these, you'll need to use a different
fi rmware, like leg OS or pbForth.
In lines
N QC does offer another interesting option, the inline subroutine. In source code, it looks a lot like a subroutine except with a
C-style return type (always void):
void wiggle() {
OnFwd(OUT_A);
OnRev(OUT_C);
Wait(20);
OnFwd(OUT_C );
OnRev(OUT_A);
Wait(20);
Off(OUT_A + OUT_C);
}
Inlines are cal led the same way as subroutin es. The compiler actually places the code of the inline wherever it is called, almost
like a ma cro or co nstant definition. This actually makes inlines appear a little more capable than subroutines: they can call
other inlines or even subroutines. In NQC v ersion 2.0, for example, you can define inlines with a parameter, like this:
voi d wigg leTime(int waitTime) {
OnFwd(OUT_A);
OnRev(OUT_C);
Wait(waitTime);
OnFwd(OUT_C);
OnRev(OUT_A);
Wait(waitTime);
Off(OUT_A + OUT_C);
}
You can have more than one argument, if you wish. Just remember that in lines are really an example of syntactic sugar,
something that makes your source code look pretty but doesn't necessarily result in b etter efficiency.
Arguments to inlines can be p assed in four different ways. Table 4-7 summarizes the options.