ADD

Returns the sum of two or more inputs.

Signature

     ┌─────────┐
IN1 ─┤         │
IN2 ─┤   ADD   ├─ OUT
IN3 ─┤         │
     └─────────┘
FUNCTION ADD : ANY_NUM
  VAR_INPUT
    IN1 : ANY_NUM;
    IN2 : ANY_NUM;
    (* ... additional inputs ... *)
  END_VAR
END_FUNCTION

The return type matches the input type. ADD accepts SINT, INT, DINT, LINT, USINT, UINT, UDINT, ULINT, REAL, LREAL. Inputs of different numeric types widen to the wider one, which is also the return type; see Type Conversions. The time and date types are covered by the overloads below.

Inputs

Name

Type

Description

IN1

ANY_NUM

The first addend.

IN2

ANY_NUM

The second addend.

Outputs

Name

Type

Description

Return value

ANY_NUM

The sum of all inputs. Same type as the inputs.

Description

Returns the sum of all inputs. ADD(a, b) is the functional form of the + operator: a + b. Both forms are equivalent, and ADD(a, b, c) is a + b + c.

For integer types, overflow behavior wraps around (modular arithmetic).

Time and date overloads

ADD and the + operator are also defined on the following time and date operands (IEC 61131-3 Table 30). Each combination is the typed function in the last column and computes what it computes.

IN1

IN2

Return value

Same as

TIME

TIME

TIME

ADD_TIME

TIME_OF_DAY

TIME

TIME_OF_DAY

ADD_TOD_TIME

DATE_AND_TIME

TIME

DATE_AND_TIME

ADD_DT_TIME

Folding applies here too: ADD(t1, t2, t3) is t1 + t2 + t3, two ADD_TIME steps.

Each typed function has a long form over LTIME, LDATE, LTIME_OF_DAY and LDATE_AND_TIME (ADD_LTIME, ADD_LTOD_LTIME, ADD_LDT_LTIME), which applies when either operand is of a long type.

Any other combination of types is an error (P4049).

Example

result := ADD(10, 20);      (* result = 30 *)
result := 10 + 20;          (* result = 30, operator form *)
result := ADD(10, 20, 30);  (* result = 60 *)

See Also

  • SUB — subtraction

  • MUL — multiplication

  • DIV — division

References