FUNCTIONs and SUBs are used to "encapsulate" sections of code and make it easy to reuse that code in the same app or in different apps via copy-paste, dlls(dynamic linked library) or slls(static linked library). The difference between a function and a sub is that the function returns a value (or not) whereas a sub can not return a value.
Functions have two forms, numeric and alpha(string).
Numeric function:
FUNCTION FN.SomeNumer(arg1, arg2 , arg3$, ....) '<--- input values
'<--- variables used are local to the code block unless they were declared GLOBAL elsewhere
some code
FN.SomeNumber = A.number '<--- return value
END FUNCTION
Alpha function:
FUNCTION FN.SomeNumber$(arg1$, arg2, arg3)
'<--- variables used are local to the code block unless they were declared GLOBAL elsewhere
some code
FN.SomeNumber$ = A.string$ '<--- return value
END FUNCTION
The above indicates that a function can return only one value; however, by modifying the input values with the BYREF declaration, more than one value can be returned. This is also the case for SUB routines.
Modified function arguments(input values):
FUNCTION FN.SomeNumber(BYREF Arg1, Arg2, BYREF Arg3$)
In this case Arg1 and Arg3$ may or may not contain a value. If the they are changed inside the function, they will have the the changed value when the function completes . On the other hand if Arg2 is changed in the function it will have its original value on completion.
EXAMPLE:
'
Arg1 = 45
Arg2 = 128
Arg3$ = "THIS IS SOME TEXT"
RetVal = 0
RetVal = FN.SomeNumber(Arg1, Arg2, Arg3$)
PRINT Arg3$; " "; RetVal
WAIT
'-------------------------------------
'-------------------------------------
FUNCTION FN.SomeNumber(BYREF Arg1, Arg2, BYREF Arg3$)
Arg2 = Arg1 * Arg2
Arg1 = Arg1 Mod 3
Arg3$ = "THIS IS THE RESULT: "
FN.SomeNumber = Arg1
END FUNCTION
'
The same thing holds for SUBs.