GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Download
  • News
  • Community
  • Resources
  • Language Reference
  • SmallBASIC Manual

This view

  • Original page
  • New address

GeezerVison is showing smallbasic.github.io — SmallBASIC | DEF

Edit History

DEF

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

Home > Reference > Language

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
Code 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  
This page was last edited on Thu, 19 Oct 2023 15:33:38 +0200 | Markdown processed with pandoc 3.1.12.1
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.