Functions
While writing programs you may need to repeat the same sequence of instructions in several parts of the program. sdlBasic offers you the chance of write the instruction sequence one time and then recall it from every part of the program may needs it.
Variables used inside the function are local variables, that means they will not influence other variables that happens to use the same variable name in your main program or in any other function. This is valid for arrays too.
See Global Shared Dim Commons statements for more details.
You can pass to the function a list of parameters containing the informations to be processed from the function itself. Functions that do not require input parameters can be called from your main program without using brackets. Functions can optionally return values,specified by the Return statement.
sdlBasic borrows the return statement from C. In addition to the standard method of returning values from functions:
function add( a, b )
add = a + b
end function
you can also write:
function add( a, b )
Return a + b
end function
sdlBasic exits the function at the point the return statement is executed.
You can simply call the function by the following statement:
newSumm= add( 3, 4)
in this example, variable newSumm will be set to 7 as returned buy function.
You can choose to simply ignore the result of a function, and treat it like a subroutine:
' Ignore the return value of Len()
len("123")
the returned value will be lost.
Function name can't be the same of variables or arrays used in the program with the same scope. This mean you can't use the following code:
myVariable = 1
function myVariable()
...
end function
but you are allowed to use this:
function myFuntion()
myVariable=1
end function
function myVariable()
...
end function