I'm facing a pretty much doubt regarding functions in B4A.
If we want a Sub - in a module - to return 2 values in the Main, for instance, can we make the following routine:
Sub Function1 (sinput as double, sout1 as double, sout2 as double)
sout1 = 2 * sinput
sout2 = 3 * sinput
End Sub
?
It doesn't seem to work. Must we use Module1.sout1 and Module1.sout2 to retrive the values? But then, why should we pass them to the Sub?
This is pretty much standard in all languages, you only return one value.
However, that one value can be an object, which, in turn, can contain many values.
For example, say that you want to return coordinates for something. Instead of returning X, Y and Z, you define a class which has X, Y and Z as properties, and returns an object of that class. If you don't want any logic in the class, a type works as well.
I'm facing a pretty much doubt regarding functions in B4A.
If we want a Sub - in a module - to return 2 values in the Main, for instance, can we make the following routine:
Sub Function1 (sinput as double, sout1 as double, sout2 as double)
sout1 = 2 * sinput
sout2 = 3 * sinput
End Sub
?
It doesn't seem to work. Must we use Module1.sout1 and Module1.sout2 to retrive the values? But then, why should we pass them to the Sub?
The normal way to return a value from a Function if not using a Global variable is the way that @DonManfred has demonstrated above. That is to declare the name of the Function followed by any input parameters contained within parenthesise and after that declare the return date type. In the function you then use the Return keyword to pass a value back to the calling sub.