Richard Russell wrote: ↑Tue 09 Dec 2025, 18:03
Anything placed on the DEF PROC line will be executed only when that procedure is called, not when execution 'falls through' from above.
This is a commonly-used technique to simulate, in a way, a polymorphic function - i.e. a function which can take different numbers or types of parameters.
Suppose one has a library function that exists in four forms: a base function that takes a set of parameters, a variant of that function which takes an additional parameter
type%, a second variant which takes an additional parameter
flag% and a third variant which takes both those additional parameters. When the additional parameters are not supplied they default to zero.
Here's how that can be implemented:
Code: Select all
DEF PROC_polymorphic(parameters) : LOCAL type%, flag%
DEF PROC_polymorphic_1(parameters, type%) : LOCAL flag%
DEF PROC_polymorphic_2(parameters, flag%) : LOCAL type%
DEF PROC_polymorphic_3(parameters, type%, flag%)
REM Common code here....
ENDPROC
Of course if you want the omitted parameter(s) to default to something other than zero, you can add appropriate initialisation - also on the DEF PROC line(s):
Code: Select all
DEF PROC_polymorphic(parameters) : LOCAL type%, flag% : type% = 1 : flag% = 2
DEF PROC_polymorphic_1(parameters, type%) : LOCAL flag% : flag% = 2
DEF PROC_polymorphic_2(parameters, flag%) : LOCAL type% : type% = 1
DEF PROC_polymorphic_3(parameters, type%, flag%)
REM Common code here....
ENDPROC
Unlike 'true' polymorphic functions you must explicitly call the variant you need, rather than the language working it out from the number and types of parameters supplied, but it's as close as you can get in BBC BASIC.