Script Functions

Overview

In this tutorial you will learn

  • what a function is
  • how to define a function in flowScript
  • how to use this function

What is a function

Functions define a reusable part of a script. Functions may be called with arguments as their inputs and optionally return some value. Functions can be used to structure a script and make scripts easier to understand.

As scripts may become longer and more complex functions provide a mean to tackle the complexity by the Divide and Conquer Principle. This means that a complex problem is divided into smaller problems until they can be solved. For example a long script can be split up into logical sections (e.g. functions). Each function can then be tested independently.

How to Define a Function

Please start FlowModeler and open the Script-IDE with Tools/Script-IDE in the Menubar.

In the Browser on the right side you can find the function item in Statements. Double-Click on the function item and a skeleton of a function is added to the Text-Editor:

function name( arguments )
    ...
    return value
endfunction

The function starts with the mandatory function keyword. Then follows the name of the function. The arguments within the parenthesis () are optional. The three dots (...) are only a placeholder and must be replaced with the actual function code. A function may also return some value. A function definition must end with the endfunction keyword.

We now use this function skeleton to define our own function. The function we define is supposed to compute the area of an rectangle.

First we replace the name in the skeleton with area:

function area( arguments )
    ...
    return value
endfunction

The function needs two inputs for the computation: a width and a height. Therefore the arguments placeholder is replaced accordingly:

function area( width, height )
    ...
    return value
endfunction

The first thing a function should do is to check the inputs it received for valid values and types. If an input is invalid it is a good idea to make the script fail with an error message. If the function would otherwise continue on an input value it is not designed to handle it might return a wrong value that makes no sense or lead to an incorrect result. Please remember: "Dead scripts tell no lies!"

As inputs for width and height In the area function we need positive Real numbers:

function area( width, height )
    assert( type.isReal( width ), "'width' must be a Real number" )
    assert( type.isReal( height ), "'height' must be a Real number" )
    assert( width greater 0, "'width' must be positive" )
    assert( height greater 0, "'height' must be positive" )
    ...
    return value
endfunction

 Now we can compute the area of the rectangle and return it:

function area( width, height )
    assert( type.isReal( width ), "'width' must be a Real number" )
    assert( type.isReal( height ), "'height' must be a Real number" )
    assert( width greater 0, "'width' must be positive" )
    assert( height greater 0, "'height' must be positive" )
    value = width * height
    return value
endfunction

So the function is now defined and can be used.

Use the defined function

In order to use the area function, we simply add some calls in the script below the function definition:

print( area(10.0, 20.0) )
print( area(100.0, 200.0) )
print( area(5.5, 2.8) )
print( area(10.3, 20.2) )

When the script is executed now with Script/Play you will see the following Console output:

Preprocessing Script...
Register function 'area'
Executing Script...
200
20000
15.4
208.06
Executing Script Done...
Cleaning up after script execution...

You can see that the area function has been registered during the Preprocessing of the script and printed the computed area values during the Execution.

You can also check that the script fails when, for example, a negative or a zero value is given as one of the arguments. Also check what happens when you give a string or an integer to the function:

print( area(-10.0, 20.0) )
print( area(100.0, -200.0) )
print( area(5.5, "a string") )
print( area(5, 12.5) )

Conclusion

Functions are a very powerful tool in scripting. You now know how to define a function and reuse it in a script. Later we will also see that functions are not limited to a single script file but can also be imported from other scripts. This allows to define functions and use them as libraries. But more on that in another tutorial.

Downloads