
Page 75
efficient because the code is just compiled once. With a macro, the entire macro body would be placed at each point where it was called.
The 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. Third, no more than 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 firmware, like legOS or pbForth.
Inlines
NQC 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 called the same way as subroutines. The compiler actually places the code of the inline wherever it is called, almost like a macro or constant definition. This actually makes inlines
appear a little more capable than subroutines: they can call other inlines or even subroutines. In NQC version 2.0, for example, you can define inlines with a parameter, like this:
void wiggleTime(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 inlines are really an example of
syntactic sugar,
something that makes your source code look pretty but doesn't necessarily
result in better efficiency.
Arguments to inlines can be passed in four different ways. Table 4-7 summarizes the options.
Page 76
Table 4-7. Argument Passing for Inlines
Type
By Value or By Reference?
Temporary Variable Used?
int
by value
yes
const int
by value
no
int&
by reference
no
const int&
by reference
no
Содержание MINDSTORMS Robots
Страница 22: ...Page 18 The back tread wheels are anchored to the shafts with the 16t gears ...
Страница 23: ...Page 19 Page 20 Next start building support for the drive motors ...
Страница 25: ...Page 22 Attach the motor wires to output A and output C Next build the platform for the front bumpers ...
Страница 26: ...Page 23 The touch sensors are attached only by the shaft that runs through them ...
Страница 41: ...Page 41 ...
Страница 43: ...Next build the support for the light sensor ...
Страница 80: ...Page 85 Make sure the bump on the long pin is up against the 4u beam ...
Страница 82: ......
Страница 84: ...Page 89 ...
Страница 85: ...Step 14 is similar to Step 11 take a deep breath and go slowly ...
Страница 86: ...Page 90 Grabber Arm In Step 17 the half bushings go between the center block and the cams pear shaped pieces ...
Страница 87: ...Page 91 ...
Страница 88: ...Page 92 Make sure that the two sides are at the same angle They should mirror each other ...
Страница 89: ...Page 93 Page 94 ...
Страница 90: ...Structural Support Page 95 ...
Страница 91: ...Idler Wheel Page 96 ...
Страница 92: ...Page 97 Drive Motor ...
Страница 93: ...While you re putting the motor in hold on to the worm gear so it doesn t slip off Page 98 ...
Страница 94: ...Grabber Arm Motor ...
Страница 95: ...Page 99 ...
Страница 96: ...Page 100 RCX Attach the RCX on both sides as shown Page 101 ...
Страница 158: ......
Страница 159: ...Page 175 The 16t gears are nestled inside the tread wheels ...
Страница 160: ...Page 176 ...
Страница 161: ...Page 177 Attach the motors to output A and output C as shown Page 178 ...
Страница 162: ...The light sensor which is mounted on the bumper is attached to input 2 The touch sensor goes on input 1 Page 179 ...