GOOC - Game Oriented Object C
GOOC - Game Oriented Object C
9.5. Inline Subroutines
9.5. Inline Subroutines
A subroutine may be marked as inline by prefacing it with inline sub instead of sub. Such subroutines are not compiled into GOOL instructions, instead the compiler stores them as a series of compiler instructions. Calling an inline subroutine will not output a call instruction with parameters, instead the contents of the called subroutine will be placed almost verbatim where they were called from (in their own block), and the parameters in the call will verbatim replace the parameters in the inline subroutine. Inline subroutines are one of the most powerful development tools in GOOC, as they allow you to quickly segment your code in a more natural way which doesn't incur any overhead of calling functions, or wrapping a series of statements (or just a single, more complex statement) into a single line that may otherwise be more tedious to copy around, code readability is improved and error proneness is reduced.
Take the following inline subroutine:
inline sub Foobar(a) {
x += a
y += a
}
Now, this piece of code,
...
FooBar(50.0)
...
will be turned into
...
{
x += 50.0
y += 50.0
}
...
Keep in mind the inline sub parameters are replaced, but not evaluated, so a more volatile expression like rand(50.0) may have unexpected side effects:
...
{
x += rand(50.0)
y += rand(50.0)
}
...
Instead of being called only once and having its return value stored, rand(50.0) will instead be called twice, and the result will be different each time.
Inline subroutines are subject to a few rule changes compared to regular subroutines:
- Inline subroutines CAN share names with other inline subroutines, as long as they have differing parameter counts. This is referred to as overloading.
- Inline subroutine calls MUST have the correct parameter count for the subroutine trying to be called.
- Inline subroutine cannot call themselves (but can call other overloads).
- Inline subroutine can only be called if they have already been defined.
- Inline subroutines cannot be part of expressions. The compiler will simply not find them.
- Inline subroutine paramaters cannot be written to.
Inline subroutines also may use different subroutine modifiers, described in the next chapter.
A return statement inside an inline subroutine will instead be turned into an unconditional jump to after the subroutine.