.ModelicaReference.Classes.'function'

Information

Define specialized class function

Examples

function si
  input Real x;
  output Real y;
algorithm
  y = if abs(x) < Modelica.Constants.eps then 1 else Modelica.Math.sin(x)/x;
end si;
Simulation result

Syntax

   [ encapsulated ][ partial] [ pure | impure] function
   IDENT class_specifier

class_specifier :
   string_comment composition end IDENT
   | "=" base_prefix name [ array_subscripts ] [ class_modification ] comment
   | "=" enumeration "(" ( [enum_list] | ":" ) ")" comment

See Modelica Grammar for further details.

Description

The keyword function is used to define functions as known from programming languages.

The syntax and semantics of a function have many similarities to those of the block specialized class. A function has many of the properties of a general class, e.g., being able to inherit other functions, or to redeclare or modify elements of a function declaration.

Modelica functions have the following restrictions compared to a general Modelica class:

Modelica functions have the following enhancements compared to a general Modelica class:

A function may have a function as an input argument. The declared type of such an input formal parameter in a function can be the class-name of a partial function that has no replaceable elements. It cannot be the class-name of a record [i.e., record constructor functions are not allowed in this context.] Such an input formal parameter of function type can also have an optional functional default value. Example:

function quadrature "Integrate function y=integrand(x) from x1 to x2"
  input  Real x1;
  input  Real x2;
  input  Integrand integrand;   // Integrand is a partial function, see below
  // With default: input Integrand integrand := Modelica.Math.sin;
  output Real integral;
algorithm
  integral :=(x2-x1)*(integrand(x1) + integrand(x2))/2;
end quadrature;

partial function Integrand
  input  Real x;
  output Real y;
end Integrand;

A functional argument can be provided in one of the following forms to be passed to a formal parameter of function type in a function call (see examples below):

  1. as a function name,
  2. as a function partial application,
  3. as a function that is a component,
  4. as a function partial application of a function that is a component.

In all cases the provided function must be "function type compatible" to the corresponding formal parameter of function type. Example:

// A function as a positional input argument according to case (a)
function Parabola
   extends Integrand;
algorithm
   y = x*x;
end Parabola;

area = quadrature(0, 1, Parabola);

// The quadrature2 example below uses a function integrand that
// is a component as input argument according to case (c):
function quadrature2 "Integrate function y=integrand(x) from x1 to x2"
  input  Real x1;
  input  Real x2;
  input  Integrand integrand;   // Integrand is a partial function type
  output Real integral;
algorithm
   integral := quadrature(x1,       (x1+x2)/2, integrand)+
               quadrature((x1+x2)/2, x2,       integrand);
end quadrature2;

Contents

NameDescription
 'function partial application'function partial application
 'pure function'pure function

Generated at 2024-03-28T19:15:55Z by OpenModelicaOpenModelica 1.22.3 using GenerateDoc.mos