@@ -123,8 +123,37 @@ o100 endsub
123123o100 call [100] [2] [325]
124124----
125125
126- Subroutine bodies may not be nested.
127- They may only be called after they are defined.
126+ Subroutine *definitions* may not be nested.
127+ Defining a subroutine inside another subroutine is not allowed and will
128+ produce an error. For example, the following is *invalid*:
129+
130+ .Invalid Nested Subroutine Definition (produces an error)
131+ [source,{ngc}]
132+ ----
133+ o<outer> sub
134+ o100 sub (INVALID: nested subroutine definition)
135+ (code here)
136+ o100 endsub
137+ o<outer> endsub
138+ ----
139+
140+ However, *calling* a subroutine from within another subroutine is perfectly
141+ valid. It is the `sub`/`endsub` definition that cannot be nested, not the
142+ `call`:
143+
144+ .Valid Nested Subroutine Call
145+ [source,{ngc}]
146+ ----
147+ o<outer> sub
148+ (code here)
149+ o<helper> call [1] [2] (OK: calling another sub)
150+ o<outer> endsub
151+ ----
152+
153+ Other O-code control flow such as `if`/`endif`, `while`/`endwhile`,
154+ `do`/`while`, and `repeat`/`endrepeat` may be used freely inside subroutines.
155+
156+ Subroutines may only be called after they are defined.
128157They may be called from other functions, and may call themselves recursively if it makes sense to do so.
129158The maximum subroutine nesting level is 10.
130159
@@ -391,6 +420,10 @@ your call and include a sub and endsub in the file. The file must be in the
391420directory pointed to by 'PROGRAM_PREFIX' or 'SUBROUTINE_PATH' in the INI file.
392421The file name can include *lowercase* letters, numbers, dash, and underscore
393422only. A named subroutine file can contain only a single subroutine definition.
423+ Do not place additional numbered subroutine definitions (e.g. `o100 sub`)
424+ in the same file as a named subroutine. Numbered subroutines share a global
425+ namespace and can silently conflict across files. If you need helper
426+ subroutines, place each one in its own file.
394427
395428.Named File Example
396429[source,{ngc}]
@@ -455,6 +488,9 @@ interpreter:
455488 - a label on `else`, `elseif` or `endif` not pointing to a matching `if`
456489 - a label on `break` or `continue` which does not point to a matching `while` or `do`
457490 - a label on `endrepeat` or `endwhile` no referring to a corresponding `while` or `repeat`
491+ - a subroutine definition (`sub`) inside another subroutine body (nested definitions)
492+ - a numbered subroutine called from within a named subroutine file but not found in the
493+ offset table or as a separate file
458494
459495To make these errors non-fatal warnings on stderr, set bit 0x20 in
460496the `[RS274NGC]FEATURE=` mask ini option.
0 commit comments