Home > Language Reference > Program Flow Functions > Function

Function

The Function...EndFunction statement declares the name, parameters and included statements that define a procedure (sub-program). A function can return a value by assigning a value to the function name.

Usage

Used to process the same actions at different points in a script.

Syntax

Function <function_name>([<param1> [, <param2>]....])
        [Local <variable_name>]
        <statements>
        ...
        <function_name>=<value>
        [ExitFunction]
        ...
    EndFunction


Parameters

<function_name>, label, name of the function. Max length of a function name is 32 characters.

<param1>, <param2>, optional string or integer, parameters of the function. Parameters are passed by reference, not by value.

Local, keyword. Used to define Local variables inside the function.

ExitFunction, keyword. Used to exit the function at this step.

Remarks

The line
<function_name>=<value>
sets the return value for the function.

A function must be declared using Function...EndFunction statement at the beginning of the script, just after the DIM statements (if any are needed).

If the function returns a string, its name must end with a $, see the example below.

Parameters cannot be arrays. See below an example code demonstrating how to pass arrays in a function using a global variable.

See also

Sub
Script Structure
Global and Local Variables

Examples

'Declare a function which returns the absolute value of the difference between two integers
Function value_absolute_dif(int1, int2)
    if int1 > int2 then
        'Set the result
        value_absolute_dif = int1 - int2
    else
      if int1 = int2 then
      'Set the result  
      value_absolute_dif = 0
    else
      'Set the result
      value_absolute_dif = int2 - int1
    endif
    endif
Endfunction 

'Function call anywhere in the script
    res = value_absolute_dif(-367,123)
    'Display the result
    msgbox(res)     'res contains 490



'Declare a function which returns a string (the function name ends with the $ character), it concatenates 2 strings.
Function concat$(string1$,string2$)
    'Declare result$ as a local variable within the function
    local result$
    result$=string1$+string2$
    concat$=result$
EndFunction

'Function call anywhere in the script
    msgbox(concat$("Hello ","WinTask"))



'Function using arrays
'Declare first the arrays using Dim statement
Dim buffer$(100)
dim k1$(100)
dim k2$(100)
    
'Declare the function which inserts nb_row values in A column of an Excel file specified in fileexcel$ parameter
function insert_values(fileexcel$,nb_row)
    i=1
    repeat
        writeexcel(fileexcel$,"A1:"+"A"+str$(i),buffer$())
        i=i+1
    until i=nb_row
endfunction 

'Function call, but first fill the global variable buffer$ with needed data
k1$(0)="text1"
k1$(1)="text2"
buffer$()=k1$()
'The Excel file must exist or use CreateExcelFile function to create it before the function call
insert_values("c:\wttest\myexcel.xlsx", 10)

k2$(0)="mytext1"
k2$(1)="mytext2"
k2$(3)="mytext3"
buffer$()=k2$()
insert_values("c:\wttest\myexcel.xlsx",30)