DEF

DEF name[(par1 [, … , parN])] = expression

Defines a single line function with the function name name and the parameters par1 to parN. expression is a valid SmallBASIC expression returning a value.

See FUNC for multiline functions and SUB for multiline subroutines.

Example 1: Function with one parameter

def f(x) = 2*x+3
print f(3)

Example 2: Function with two parameters

def f(x,y) = 2*x + 3*sin(y)
print f(1,2)

Example 3: Random number between low and high

' A function for random integer numbers between low and high (inclusive)
DEF randi(lo, hi) = (RND * (hi - lo + 1))\1 + lo

' A function for random float numbers between low and high (inclusive)
DEF randf(lo, hi) = (RND * (hi - lo + 1)) + lo

print randi(0, 100)
print randf(0, 100)

Example 4: DEF and LOCAL

' DEF and LOCAL.bas  SmallBASIC 0.12.2 [B+=MGA] 2016-04-06
'can I use DEF locally in a sub without interfering with main variable or DEF names
def aboutMe() = "I am texting from main code."
? "Hi, ";aboutMe
mySub
? "Goodbye, ";aboutMe
pause
sub mySub()
  'local def aboutMe() = "I am texting from mySub." '<=== note: this did not work 
  local aboutMe
  def aboutMe() = "I am texting from mySub."
? "Hi, ";aboutMe
end
Language
If there is insufficient information on this page and you wish learn more about DEF, please send an email to smallbasic@gmail.com. You can help to improve information about DEF by submitting a pull request, click View Source for details. Note, an offline language reference text file is also available - see the Download section.