Call a Function Block

A  Function BlockClosed A function block groups an algorithm and a set of private data. It has inputs and outputs. groups an algorithm and a set of private data.

It has inputs and outputs.

A functionClosed A function calculates a result according to the current value of its inputs. A function has no internal data and is not linked to declared instances. block can be:

To use a function block, declare an instance of the block as a variable with a unique name.

  • Each instance of a function block has its own set of private data and can be called separately.
  • A call to a function block instance processes the block algorithm on the private data of the instance, using the specified input parameters.

  • Best Practice Put function blocks in an N step and not in P0 or P1 because those steps are executed only once.
    If you must use an FB in P0 or P1 be sure to call it again in the N state so it can finish.

FBD and FFLD Languages

To call a function block in FBDClosed Function block diagram - Describes a function between input variables and output variables. A function is described as a set of elementary blocks. Language or FFLD Language, insert the block in the diagram and to connect its inputs and outputs.

The name of the instance must be specified upon the rectangle of the block.

IL Language Example

To call a function block in ILClosed Instruction list - This is a low-level language and resembles assembly. Language, you must use the CAL instruction and a declared instance of the function block.

  • The instance name is the operand of the CAL instruction, followed by the input parameters written between parentheses and separated by comas.
  • Alternatively the CALC, CALCN or CALNC conditional instructions can be used:
Operand Description

CAL

Calls the function block.

CALC

Calls the function block if the current result is TRUE.

CALNC

Calls the function block if the current result is FALSE.

CALCN

Calls the function block if the current result is FALSE.

This example demonstrates a call to an instance of TON function block:

(* MyTimer is declared as an instance of TON *)
Op1: CAL   MyTimer (bTrig, t#2s)
FFLD    MyTimer.Q
STClosed Structured text - A high-level language that is block structured and syntactically resembles Pascal.     TimerOutput
FFLD     MyTimer.ET
ST    ElapsedTimer

Op2: FFLD  bCond
CALC MyTimer (bTrig, t#2s)  (* called only if bCond is TRUE *)
Op3: FFLD  bCond
CALNC MyTimer (bTrig, t#2s)  (* called only if bCond is FALSE *)

ST Language Example

To call a function block in ST Language, specify the name of the instance, followed by the input parameters written between parentheses and separated by commas.

To have access to an output parameter, use the name of the instance followed by a dot '.' and the name of the desired parameter.

This example demonstrates a call to an instance of TON function block:

(* MyTimer is declared as an instance of TON: *)
MyTimer (bTrig, t#2s); (* calls the function block *)
TimerOutput := MyTimer.Q;
ElapsedTime := MyTimer.ET;

See Also

Differences between Functions and Function Blocks