DEF FN - QB64 Wiki


From QB64 Wiki

Jump to: navigation, search

The DEF FN statement defines a non-recursive function in the main module that can use the module's variable values.


DEF FNname [(parameterList)] = expression
DEF FNname [(parameterList)]
FNname = expression


  • Currently NOT supported in QB64!
  • name is the name of the function.
  • parameterList is a comma-separated list of one or more parameters, similar to SUB and FUNCTION statements.
  • expression is any numerical or string value to return from the function.
  • Function can be created anywhere in a module. Not in SUB procedures.
  • Function can NOT be defined in recursive(repeated) parts of a program. Declare it once only!
  • Function returns should be assigned to a variable when an alteration to the return value is necessary.
  • Function references should only be made AFTER the declaration.
  • Multi-line function definitions MUST end with END DEF.
  • To leave a DEF Fn function early use EXIT DEF.
  • This type of function can share values in module level code. To keep values local to the procedure use STATIC.
  • Parameters cannot be from arrays, records or fixed length strings!
  • Fn must prefix the function name inside of the procedure and in a call.
  • Other variable names or procedures cannot begin with FN or fn in Qbasic!
  • Qbasic Help recommends creating real FUNCTIONs over DEF FN functions because of the above limitations.

Code Examples:

Function to get the base 10 logarithm of a number value.

DEF FNLog10#(x) = LOG(x) / LOG(10.#) PRINT FNLog10#(10) PRINT FNLog10#(100) PRINT FNLog10#(1000)

1 2 3

Demonstrates the multi-line syntax by drawing a circle:

CONST PI# = 3.141592653589793# DEF FNDegreesToRadians# (degrees#) radians# = degrees# * (PI# / 180#) FNDegreesToRadians# = radians# END DEF SCREEN 13 WINDOW (-1, -1)-(1, 1) CLS FOR degree% = 0 TO 360 x# = COS(FNDegreesToRadians#(degree%)) y# = SIN(FNDegreesToRadians#(degree%)) PSET (x#, y#) NEXT degree%

See also:

Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page
Personal tools