LoginLogin

Optional Arguments in User Defined Functions

Root / General / [.]

12Me21Created:
SmileBasic supports optional arguments in all functions that don't have ( ). (the functions themselves don't always, though)
DEF TEST A,B:END

TEST 7,
works HOWEVER, in this example, using B will cause errors in many functions.
DEF TEST A,B: PRINT B :END
TEST 7,
causes a TYPE MISMATCH error. You can avoid this by using code like this:
LOCATE 0, :LOCATE B,
B2=CSRX
Now, if B is not specified, it will default to 0. (use B2; unspecified variables are dangerous) Of course, this only works within the limitations of LOCATE. I am currently working on a better system, but you can do some things without any fix. For instance, here is a locate/print function:
DEF OUTPUT X,Y,S$
LOCATE X,Y:PRINT S$
END
Since LOCATE allows you to omit either argument, the user defined function does too.