Copyright 2008 Shoptalk Systems

FUNCTION functionName([varName1[, varName2...]])


This statement begins the definition of a function, which always ends with the statement END FUNCTION.  For example:

  function functionName()
    'code goes here
  end function

A function can return a string value, a numeric value, or an object.

  function functionName()    'return a number
  function functionName$()   'return a string
  function #functionName()   'return an object

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.


  function functionName(var1, var2)


  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.

Global Variables and Devices

String, numeric and object variables declared with the GLOBAL statement are available in the main program and in subroutines and functions.

Arrays are global to a Run BASIC program, and visible inside a subroutine 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 DefaultDir$, Platform$, UserInfo$, UrlKeys$, UserAddress$, ProjectsRoot$, ResourcesRoot$, Err$, Err, EventKey$, RowIndex.  The value of these variables 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.


  'count the words
  input "Type a sentence>"; sentence$
  print "There are "; wordCount(sentence$); " words in the sentence."

function wordCount(aString$)
  index = 1
  while word$(aString$, index) <> ""
    index = index + 1
  wordCount = index - 1
end function

See also: SUB