DEF is important, super important. In combination with WHILE/WEND, REPEAT/UNTIL, and IF/THEN/ELSE statements that have multiple lines of code per case, you should NEVER use GOTO or GOSUB ever again. Never!
DEF as you may have guesses is short for DEFINE. We use it to define fuctions and procedures. If you return a value it is a function if you don't, it is a procedure.
The basic syntax looks like the following for a procedure with no parameters:
DEF ProcedureName
'Code goes here
END
For a function with no parameters:
DEF FunctionName()
'Code goes here
RETURN SomeValueOrString
END
Please note the parenthesis on the first line. That is how SmileBasic tells apart functions and subroutines. If you make a function, you need to return a value. You do that with the RETURN keyword.
Both can take parameters. One or more values you pass in separated by commas.
DEF ProcedureName Param1%, Param2#, Param3$
'Code goes here
END
DEF FunctionName(Param1%, Param2#, Param3$)
'Code goes here
Return SomeValue%
END
To call a function or subroutine it looks something like the following
'Procedure, no parameters
ProcedureName
'Procedure with parameters.
ProcedureName 42, PI(), UserName$
'Note you can pass in constants, variables, and the result of functions as parameters
'Function without parameters
result% = FunctionName()
'Function with parameters
result% = FunctionName(42, PI(), UserName$)
'Note the result has to be used somehow
'Passed to something else or assigned to a variable or
'otherwise used in an expression
So, why do I want to go through all this work when I am comfortable with GOTO and GOSUB you ask? There are several reasons.
1. It makes your code easier to reuse. If things are packaged up in a function or procedure it is much easier to copy and paste it into something new.
2. It reduces interdependencies in code. If you use GOTO or GOSUB, you can't pass in parameters or return results without using global variables. This makes it harder to move code around. It can also have unintended consequences when one bit of code messes up anothers variables.
3. You can now have local variables. If you want you can have two variables with the same name, one at the global level and one inside your function or procedure. SmileBasic will check from the local call to find a variable. If you have a local variable of the same name it will hide the global while you are in the function. Even better if you call a function within a function (recursion) each call gets its own set of variables. At any rate less global variables is almost always better. It is also good to not accidentally wipe out another bit of code's stuff and be able to reuse variable names without fear.
4. Recursion. You can have a function call itself which opens up a whole set of algorithms that were ackward otherwise. This relies on each call getting its own state (stack frame) as noted previously. (Yes the example could be easily changed to a loop, it is a simple example).
DEF Factorial(num%)
IF num% <= 1 THEN
RETURN 1
ELSE
RETURN num% * Factorial(num% - 1)
ENDIF
END
Print Factorial(6)
5. It makes code easier to read. If you know what to pass in and expect as a result you can treat the code in a function/procedure as a black box you don't have to worry about. This makes code easier to read. With functions and Procedures you need far fewer global variables too. Less stuff to remember. Also a function or procedure name says a lot more than a line number which helps document intent.
6. Organization, you can put code in discrete units that have defined beginnings, endings, input, and output. Each one should be small enough to have a single well defined purpose if done well.
I hope that is good enough motivation for you. It is better than GOTO and GOSUB in just about every way. User defined functions will help you understand methods in object oriented languages too. Now if only we had user defined types/structs .... if only.