Legato
Legato

GoFiler Legato Script Reference

 

Legato v 1.4j

Application v 5.22b

  

 

Chapter FourFlow Control (continued)

4.10 Script Functions

4.10.1 Overview

Functions are fundamental to efficient programming. A function (also called a subroutine) can be thought of as a sequence of program statements packaged as a unit that perform a specific task. Using a function is often referred to as calling it, and the line of code that performs that operation is a function call. Functions allow for repetitive operations within a program unbound by loops or conditional statements. For example, a function that performs text parsing could be called while importing the data, editing the data, or saving the data.

Functions also improve program organization. It would be rather inefficient to have the same sequence of operations repeated in the code each time it’s needed, as would have to be done with the above example when importing, editing, and saving text without a function. In addition, making changes becomes laborious. A function allows that task to be modularized. This also permits the calling code to operate without any knowledge of how the function actually works; all that is necessary is knowledge of the function’s input parameters and return values.

When a function is called, program execution switches to the called function. It continues through the called function and its control paths (which can be very simple or complex and can include calls to other functions as well) until the end of the function is reached. This can be through the end of the function’s code or a return or exit statement. Program execution then returns (with a value, if desired) to the original calling location.

There are two types of functions in Legato: embedded functions and user-defined functions. Embedded functions are part of the Legato API; their internal operation is unavailable to the programmer. The programmer can interface with these functions via their input parameters and return values. Legato functions are named using camel case conventions. User-defined functions are functions named, designed, and implemented as part of the script. Their operation is controlled by the programmer.

4.10.2 Prototypes

A function’s prototype is its declaration, where the name of the function, its parameters, and its return type are specified. Much like declaring a variable, a function must be declared before its first use. A prototype appears as follows:

return_type function_name (param1, param2, ... param3);

The return_type can be any data type, including cluster types. The return type can also be void, meaning no return value will be specified. The function_name follows the naming conventions for variable names. In the parenthetical is the parameter list, which can contain any number of parameters. A parameter is both the name of the parameter and its data type.

4.10.3 Passing Parameters

Functions take parameters as input variables. Parameters are the way in which a calling operation can pass information to a function. Parameters can be of any variable type and a function can take any number of them. In Legato, parameters are passed by value unless otherwise specified. This means that a copy of the value of the variable is passed to the function rather than the variable itself. [Passing the actual variable is known as passing by reference. It is possible to pass a parameter by reference if the function accepts it.] The data, once passed to the function, is referred to as an argument.

A function’s arguments are by definition local variables; they only exist within the scope of the function. Any changes made to the values of the arguments will therefore not be available to the calling operation unless those values are passed back via a return statement. [Parameters passed by reference require the & operator. Passing a parameter by reference simply adjusts the scope of the variable to encompass the called function, rather than passing the parameter’s memory address as is done in C or C++. This is particularly useful when sending a large string to functions, as passing this by value could be slowed by copying the string repeatedly in memory.]

Passing variables to functions allows tighter control of the value of the variable through its scope. Global variables may be available to a function; however, if the function alters the value of the global variable, it will be affected everywhere else it is used in the program. This is often not the desired outcome. Passing parameters to a function allows that data, and all alterations to it, to be contained within the function.

Arrays cannot be passed as function parameters.

4.10.4 Returning Values

Functions return to the calling routine, typically with a return value. A return value can be any of the typical data types. Return values can contain error codes or the result of the function’s operations. The return statement is used to return a value to the calling routine.

Note that all control paths within a function must return a value.

Arrays cannot be return as function result.