Solved

# Create a FUNCTION with multiple input and output.

• 3 replies
• 20 views

Hi! Does anyone know how to create a function with multiple inputs and outputs?

For example, there is a function edited with MATLAB that :“function n dP,dQ,Pi,Qi ] = Unbalanced( n,m,P,Q,U,G,B,cita )”. How to model this function in AIMMS? Thank you very much!

A function can only have one return value.

However, if you want to have multiple output values, you can also do so by making some of the arguments InOut  or Out. This way, if the values of these are changed in the body of the function, the calling function will be able to access the new values in the parameters provided as arguments.

​ Hi @Jingfan Liu , how are you?

You can use Procedures to achieve it, see more in this documentation (https://documentation.aimms.com/language-reference/procedural-language-components/procedures-and-functions/internal-procedures.html).

To summarize it, you can either add them through the Arguments button, selecting the identifier type, the property and a unit. Or by simply adding between brackets the already existing identifiers directly on Arguments' Procedure setting.

Yet another alternative is to return a vector instead of a scalar.

Let the set s_a be defined as { dp, dq, pi, qi } and i_a be an index in s_a, then you can define a function that returns a 1-dimensional parameter indexed over i_a as follows:

`Function vecFunc {    Arguments: (a,b);    IndexDomain: i_a;    Body: {        ! data { dp, dq, pi, qi }        temp('dp') := 1 + a ;        temp('dq') := 1 + b ;        temp('pi') := a + b ;        temp('qi') := a * a ;                vecFunc(i_a) := temp(i_a) ;    }    Parameter a {        Property: Input;    }    Parameter b {        Property: Input;    }    Parameter temp {        IndexDomain: i_a;    }}`

subsequently, you can use such a function like:

`   p_d(i_a) := vecFunc( 3, 4)(i_a);`

this would result in:

`    p_d := data { dp : 4,  dq : 5,  pi : 7,  qi : 9 } ;`

For your convenience, I attached the project that contains this example.