Multiple Return Values in Modelica

Modelica supports multiple return values through the use of tuples. This feature can be used to return both result and error values from a function.

function vals
  output Integer a;
  output Integer b;
algorithm
  a := 3;
  b := 7;
end vals;

function main
protected
  Integer a;
  Integer b;
  Integer c;
algorithm
  // Here we use the 2 different return values from the
  // call with tuple assignment.
  (a, b) := vals();
  Modelica.Utilities.Streams.print(String(a));
  Modelica.Utilities.Streams.print(String(b));

  // If you only want a subset of the returned values,
  // you can ignore one of the values by not assigning it.
  (,c) := vals();
  Modelica.Utilities.Streams.print(String(c));
end main;

In this Modelica code:

  1. We define a function vals() that returns two integers using multiple output parameters.

  2. In the main function, we call vals() and assign its return values to a and b using tuple assignment.

  3. We print the values of a and b using Modelica.Utilities.Streams.print(), which is Modelica’s equivalent to printing to the console.

  4. To demonstrate ignoring one of the return values, we call vals() again but only assign the second value to c, effectively ignoring the first return value.

  5. Finally, we print the value of c.

When you simulate this model, it should produce the following output:

3
7
7

Note that Modelica is primarily used for modeling and simulation, so the concept of a standalone executable program is different from languages like Go. Instead, you would typically include this code as part of a larger model and simulate it using a Modelica simulation environment.