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:
We define a function
vals()that returns two integers using multiple output parameters.In the
mainfunction, we callvals()and assign its return values toaandbusing tuple assignment.We print the values of
aandbusingModelica.Utilities.Streams.print(), which is Modelica’s equivalent to printing to the console.To demonstrate ignoring one of the return values, we call
vals()again but only assign the second value toc, effectively ignoring the first return value.Finally, we print the value of
c.
When you simulate this model, it should produce the following output:
3
7
7Note 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.