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
endCode samples using DEF
    
      004 loops.bas  
      100lines.bas  
      3d_torus.bas  
      3dtorus.bas  
      analog clock.bas  
      Another center finder.bas  
      biorythms.bas  
      blackbox.bas  
      Blackbox.bas  
      bolmo.bas  
      calc.bas  
      Chaos_1xt.bas  
      checkers.bas  
      chess.bas  
      circ.bas  
      circumscribe triangle.bas  
      clock.bas  
      clock.bas  
      conrec-sb-v01.bas  
      dogstar5.bas  
      drawpoly.bas  
      Eliza.bas  
      eliza.bas  
      falling blocks - tetris.bas  
      form demo.bas  
      g2 Life.bas  
      g3 Life.bas  
      galgenmann.bas  
      GUI RPSLS v2.bas  
    
  Language
    
      AND  
      AS  
      BAND  
      BG  
      BOR  
      BYREF  
      CALL  
      CASE  
      CATCH  
      CONST  
      DECLARE  
      DEF  
      DO  
      ELIF  
      ELSE  
      ELSEIF  
      END  
      END TRY  
      ENDIF  
      EQV  
      EXIT  
      FALSE  
      FI  
      FOR  
      FUNC  
      GOSUB  
      GOTO  
      IF  
      IFF  
      IMP  
      IN  
      LABEL  
      LET  
      LIKE  
      LOCAL  
      LSHIFT  
      MDL  
      MOD  
      NAND  
      NEXT  
      NOR  
      NOT  
      ON  
      OR  
      REM  
      REPEAT  
      RETURN  
      RSHIFT  
      SELECT  
      STEP  
      STOP  
      SUB  
      THEN  
      THROW  
      TO  
      TRUE  
      TRY  
      UNTIL  
      USE  
      USG  
      USING  
      WEND  
      WHILE  
      XNOR  
      XOR