DEF FN - QB64 Wiki

DEF FN

From QB64 Wiki

Jump to: navigation, search

This page is maintained for historic purposes. The keyword is not supported in QB64. Create functions using FUNCTION. If older code contains DEF FN functions, they must be adapted to be compiled with QB64.




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


Contents

Syntax

DEF FNname [(parameterList)] = expression
or,
DEF FNname [(parameterList)]
[statements]
FNname = expression
[statements]
END DEF


Description

  • 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 must be defined only once and the definition cannot be in recursive (repeated) parts of a program.
  • 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.


QBasic/QuickBASIC

  • 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.


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



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