Methods and functions

Workflow language is a procedural language and any workflow program defines at least one method or function.

Methods and functions are self-contained chunks of code that perform a specific task. You give to a method (or function) a name that identifies what it does, and this name is used to “call” the function to perform its task when needed.

A method can take any number of inputs (including zero) and executes its code and does not return a value, on the other hand a function behaves just like a method the only difference being that it returns a value. 

Here is an example of a method that increments a counter:

//Method definition method incrementCounter() { var c = GET counter(1); //1 = the key of the counter c.count = c.count + 1; PUT c; } //Calling the method this->incrementCounter();

Here is an example of a function that concatenates two strings:

//Function definition function concat(name as string, surname as string) as string { return name + ", " + surname; } //Calling the function var msg = this->concat("John", "Doe");

Defining and calling methods and functions

When you define a method (or function), you can optionally define one or more named, typed values that the method takes as input, known as parameters. For function you can also define a type of value that the function will pass back as output when it is done, known as its return type.

Every method (or function) has a method name (or function name), which describes the task that the method (or function) performs. To use a method (or function), you “call” that method (or function) with its name and pass it input values (known as arguments) that match the types of the method's (or function’s) parameters. A method's (or function’s) arguments must always be provided in the same order as the method's (or function’s) parameter list.

The function in the example below is called sayHello, because that's what is does, it take the name as input and returns that person's name, say "John" and says "Hello John". To accomplish this, you define one input parameter, a string value called name and a return type of type string, wich will contain the hello phrase:

All of this information is rolled up into the function’s definition, which is prefixed with the function keyword. You indicate the function’s return type with the as keyword, which is followed by the name of the type to return.

function sayHello(name as string) as string { return "Hello " + name; }

The definition describes what the function does, what it expects to receive, and what it returns when it is done. The definition makes it easy for the function to be called unambiguously from elsewhere in your code:

Calling methods (or functions) in Workflow is done using the this keyword, followed by -> operator, ant then the name of the method (or function). For more details check the imports chapter.

Transactions

When you define a method (or function), you can optionally define the method's (or function's) transactional behaviour. 

Methods (and functions) have the atomic and isolated keywords (before the method's (or function's) name) that help you control the transactional behaviour.

Here is an example of one transactional method:

For examples and more details about transactions make sure you first read the Working with entities chapter and afterwards read the Transactions chapter.

More about parameters and return values

Methods and functions are very flexible helping you define from the simplest utility methods (or functions) to complex data processing units of code with multiple parameters and complex return values and transaction management.

Metods and functions without parameters

Methods (or functions) are not required to define input parameters. Here’s a function with no input parameters, which always returns the same string message whenever it is called:

The function definition still needs parentheses after the function’s name, even though it does not take any parameters. The function name is also followed by an empty pair of parentheses when the function is called.

Methods and functions with multiple parameters

Functions can have multiple input parameters, which are written within the function’s parentheses, separated by commas.

The function in this example has two input parameters:

You call the sum function by passing it two int argument values in parentheses, separated by commas.