Defining Functions

A LCM function operates on values. It takes a parameter (element, tuple or structure) and produces a single result (element, tuple or structure). Both parameter and result can be user types.

For the parameters root structure you have the choice to set it as an element, a structure or a tuple:

  • Setting it as a tuple (an ordered list) means that the function call must be done giving the parameters in the correct order.
  • No importance is given to the order when you use a structure because all the fields names have to be given explicitly.

In the first example, the function BooltoInt converts a bit into an integer. This function takes a tuple as input and returns a simple element.

For the second example, the function PolartoCartesian converts polar coordinates to Cartesian coordinates. This function takes a structure as input and returns a structure.


Before you begin:
  • The workbench used to edit the logic should be opened.
  1. Click New Function and select an element of the Logic tree. .

    The Function Properties dialog box is displayed

  2. Change the name of the function. Add a comment if necessary.



  3. Set the input parameters:

    1. Click twice on Add Param
    2. Set the parameters names x and n.
    3. Select the type of the parameters using the combo.



    Tip: If Named Interface is selected, the type of the input parameter is a structure. Otherwise it is a tuple. Usually it is more convenient to use a structure whenever the function is called in the block editor (the name of the inputs and outputs are more easy to identify if their name is displayed), and a tuple whenever the function is called in a SFC+ graph (the syntax of the function is simpler).

  4. Set the result expression as shown below.

    The expression result is built from functions and constants visible in the scope of the current function. That is to say functions and constants :

    • defined in the pervasives library ( see Predefined Types and Functions)
    • or defined by the user and set above the current function in the specification tree.


  5. Click OK.

    The function is added to the tree (see a typical example below ).

  6. Click New Function and define the function PolartoCartesian as follows:

    1. Add an input parameter and check Named Interface. The type Polar is a user defined type defined in the task Defining Types.



    2. Select Result in the left frame.
    3. Check Tree Mode and create a structure of two elements as follows:



      Note: Tree Mode is more convenient if the output has a structured data type. If you check Text Mode, you have to fill the complete syntax of the structured expression {x = Polar1.r * cos Polar1.theta; y = Polar1.r * sin Polar1.theta}.

  7. Click OK.

    The function is added to the specification tree.

Now, you can insert the call of a function in a block. See Function Call in the SFC+ Editor and Function Call in the Block Editor.