Scripts vs Functions

Description

Scripts and functions are different in several important aspects.

  • Written with Xbasic

    Script
    Function
    Yes

    Yes

  • Written with Action Scripting

    Script
    Function
    Yes

    No

  • Call with parameters (arguments)

    Script
    Function
    No

    Yes

  • Receive a return value

    Script
    Function
    No

    Yes

Typically, you will use scripts under buttons and under object events. In this context, scripts solve specific problems and are not intended to be shared. Functions are more often designed to be reusable code that can be called from multiple locations. Parameters make functions able to solve a group of problems. A script has the general form:

code...
code...
code...

A function has the general form:

function name as type (parameters ... parameters)
code...
code...
code...
name = return value
end function

For example, a script might add three specific values found a form, but a function might add any three values passed to it as parameters. Here is a script under the OnDepart event of the quantity, price, and commission fields of a form.

total.value = quantity.value * price.value + commission.value
parentform:total.refresh()

Here is a function call under the OnDepart event of the quantity, price, and commission fields of a form.

total.value = calc_total(quantity.value, price.value, commission.value)
parentform:total.refresh()

See Also