MAX

Returns the largest of two or more inputs.

Signature

     ┌─────────┐
IN1 ─┤         │
     │   MAX   ├─ OUT
IN2 ─┤         │
     └─────────┘
FUNCTION MAX : ANY_ELEMENTARY
  VAR_INPUT
    IN1 : ANY_ELEMENTARY;
    IN2 : ANY_ELEMENTARY;
    (* ... INn ... *)
  END_VAR
END_FUNCTION

IEC 61131-3 declares the inputs as any elementary type (ANY_ELEMENTARY). IronPLC compares integers, reals, bit strings, BOOL, times and dates, and strings: FALSE is less than TRUE, a time or date is ordered by the duration or instant it stands for, and strings are ordered as < orders them. An enumeration, a structure or an array is not elementary, and is an error (P4026). Every input is evaluated. Of strings, the function is supported where it is the whole value assigned, and each input is a literal or a variable. Using it inside an expression, such as an argument of CONCAT, or with inputs that call a function or use a computed array index, is not yet supported.

The return type is the type that every compared or selected input widens to. The compiler converts a narrower input to that type first, so an INT and a LINT give a LINT. When no input’s type holds every other input, as with a DINT and a UDINT, the call is an error (P4049).

Inputs

Name

Type

Description

IN1

ANY_ELEMENTARY

The first value to compare.

IN2

ANY_ELEMENTARY

The second value to compare.

IN3, …, INn

ANY_ELEMENTARY

Further values to compare; the function is extensible, so any number of inputs may follow IN2.

Outputs

Name

Type

Description

Return value

ANY_ELEMENTARY

The largest of the inputs. If inputs are equal, returns that value.

Description

Returns the larger of IN1 and IN2. If both inputs are equal, the function returns that value.

Example

result := MAX(10, 20);    (* result = 20 *)
result := MAX(-5, 3);     (* result = 3 *)

A duration that is never negative:

elapsed := MAX(T#-1s, T#0s);    (* elapsed = T#0s *)

See Also

  • MIN — minimum of two values

  • LIMIT — clamp to range

  • SEL — binary selection

References