FUNCTION

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Supported in Run BASIC

Description

A function is a user defined routine, which returns a value to the program. The value can be a string or a numeric value.
The value to return must be assigned to a variable inside the function, which is identical to the functions name.
Failing to do so returns 0 (for numeric functions) or "" (for string functions).
See also, SUB and ByRef

Syntax

  1. function functionName$(zero or more parameters)
  2. function functionName(zero or more parameters)

Hints

If a function returns a string, it must have a $ (dollar-sign) appended to the name, ie: functionName$
Function always goes with parentheses. Even function without parameters would have empty parentheses.
Function names are case-sensitive, just like variables.
You cannot have functions with same names but different set of parameters.
You cannot have function with same name as array (really, JB has no means of telling if a(x) array or function call)
See scope of variables for the difference between local and global variables.

Example

Function without parameters (still needs parentheses)

print dummy$()

function dummy$()
   dummy$ = time$()
end function
' 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   'note assigning value to return
end function

Useful Procedures

' Place a useful function or sub using this keyword here