Liberty BASIC Help Online

FUNCTION
See also:  Functions and Subroutines, BYREF
 
function functionName(zero or more parameter variable names)
  'code for the function goes in here
  functionName = returnValueExpression
end function
 
Description:
This statement defines a function.  The function can return a string value, or a numeric value. A function that returns a string value must have a name that ends with the "$" character.  A function that returns a numeric value must not include a "$" in its name.  Zero or more parameters may be passed into the function.  A function cannot contain another function definition, nor a subroutine definition.  Note that the opening parenthesis is actually part of the function name. Do not include a space between the name and the opening parenthesis, or the code generates an error. 
 
Right:
function functionName(var1, var2)
 
Wrong:
function functionName (var1, var2)
 
Returning a Value
To return a value from the function, assign the value to be returned to a variable of the same name as the function.  If no return value is specified, then numeric and string functions will return 0 and empty string respectively.
 
  functionName = returnValueExpression
 
Local Variables
The variable names inside a function are scoped locally, meaning that the value of any variable inside a function is different from the value of a variable of the same name outside the function.
 
Passing by Reference
Variables passed as arguments into functions are passed "by value" which means that a copy of the variable is passed into the function.  The value of the variable is not changed in the main program if it is changed in the function.  A variable may instead by passed "byref" which means that a reference to the actual variable is passed and a change in the value of this variable in the function changes the value of the variable in the main program.
 
Global Variables and Devices
Variables declared with the GLOBAL statement are available in the main program and in subroutines and functions.
 
Arrays, structs and handles of files, DLLs and windows are global to a Liberty BASIC program, and visible inside a function without needing to be passed in.
 
Special global status is given to certain default variables used for sizing, positioning, and coloring windows and controls.  These include variables WindowWidth, WindowHeight, UpperLeftX, UpperLeftY, ForegroundColor$, BackgroundColor$, ListboxColor$, TextboxColor$, ComboboxColor$, TexteditorColor$.  The value of these variables, as well as DefaultDir$ and com can be seen and modified in any subroutine/function.
 
Branch Labels
Branch labels are locally scoped.  Code inside a function cannot see branch labels outside the subroutine, and code outside a function cannot see branch labels inside any subroutine.
 
End Function
The function definition must end with the expression "end function."
 
Executing Functions
Be sure that a program doesn't accidentally flow into a function.  A function should only execute when it is called by command in the program.
 
wrong:
    for i = 1 to 10
        'do some stuff
    next i
 
    Function MyFunction(param1, param2)
        'do some stuff
        MyFunction=value
    End Function
 
correct:
    for i = 1 to 10
        'do some stuff
    next i
 
    WAIT
 
    Function MyFunction(param1, param2)
        'do some stuff
        MyFunction=value
    End Function
 
Example Usage:
 
'count the words
input "Type a sentence>"; sentence$
print "There are "; wordCount(sentence$); " words in the sentence."
end
 
function wordCount(aString$)
  index = 1
  while word$(aString$, index) <> ""
    index = index + 1
  wend
  wordCount = index - 1
end function
 
 
See also: SUB , Recursion, Functions and subroutines


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/