how to create a function and be able to call it for returning a value.

how to create a function and be able to call it for returning a value.

athodupunoori
Participant Participant
2,370 Views
10 Replies
Message 1 of 11

how to create a function and be able to call it for returning a value.

athodupunoori
Participant
Participant

[ FlexSim 21.1.5 ]

Hi, i would like to create a function using flexscript, where when i call the function, it has to return the some value according to the calculation i put inside the function.


Example

1690314752039.png


here i am just trying to create a addition function where if i send two values, it has to save that number at specified table and return me the added value "C" for the variable "dd".

0 Likes
Accepted solutions (1)
2,371 Views
10 Replies
Replies (10)
Message 2 of 11

julie_weller
Not applicable
Accepted solution

Hi @Ankith T! You can add a function by going to Toolbox -> Modeling Logic -> User Commands. This will allow you to make a little function for your model. You can add the code at the bottom. Here's a link to the documentation for the tool: https://docs.flexsim.com/en/23.1/Reference/Tools/UserCommands/UserCommands.html#app

Let me know if you have any follow up questions!

0 Likes
Message 3 of 11

athodupunoori
Participant
Participant
if i would like to send couple of values, how to i mention that in code section?
0 Likes
Message 4 of 11

julie_weller
Not applicable

Here's an example for what you want to do:

1690316904328.png

1690316917791.png

0 Likes
Message 5 of 11

athodupunoori
Participant
Participant
Thank you, that cleared my doubt. But what if i would like to return multiple variables. such as string and double. where string tells me its an addition and double tells me the value of addition.
0 Likes
Message 6 of 11

jason_lightfoot_adsk
Autodesk
Autodesk

You can return an array or map - the latter allowing you to access the result's attribute/key values using dot notation.

Message 7 of 11

julie_weller
Not applicable

To do something like that you could create 2 functions, one that tells you the string and one that gives you the double. You could update the table with both of those values. You could write to a Global Variable. However you can only return 1 value, just as in C++

0 Likes
Message 8 of 11

julie_weller
Not applicable
Or as Jason said, you can update something that already consists of multiple elements, but they need to be a unit
0 Likes
Message 9 of 11

athodupunoori
Participant
Participant
can you send here a sample code, showing how to return values in array and how to access after returning.
0 Likes
Message 10 of 11

jason_lightfoot_adsk
Autodesk
Autodesk

Here's the map version:

mapResultFromCommand.fsm

1690385243223.pngcustomFunc code:

/**Custom Code*/
Map result;
result["calcValue"]=param(1)+param(2);
result["operation"]="Addition";
return result;

Test script:

var result=customFunc(5,9);
print(result.calcValue);
print(result.operation);

Console output:

Image.png


The version with arrays is just:

/**Custom Code*/
Array result;
result.push(param(1)+param(2));
result.push("Addition");
return result;

with test script:

var result=customFunc(5,9);
print(result[1]);
print(result[2]);

If you want to use all the array operators and methods then you need to cast it as an array not var in the test script like this:

Array result=customFunc(5,9);
print(result.pop());
print(result.pop());
print(result.length);
0 Likes
Message 11 of 11

athodupunoori
Participant
Participant
Thank you so much, this is what exactly i needed.
0 Likes