Can a sub return more than 1 value ?

TWELVE

Active Member
Licensed User
Hello,

i'm asking me if that's possible, without the use of globals.

Return value

can only transport one value, right..?

Cheers

TWELVE
 

Cableguy

Expert
Licensed User
Sure seems like it...
I triyed to pass 3 values to a sub, ok, on that...but not so easy to return them....
could not find a way to do it...but it would be a great adicion, to pass more than one value, on a return keyword....
 

taximania

Well-Known Member
Licensed User
Can't a sub return an array ??

Sorry for the short answer, just turning the computer off but saw this question.
 

Cableguy

Expert
Licensed User
not unless it is declared in globals, as all arrays should be, but we are talking of single local variables...I think...
 

TWELVE

Active Member
Licensed User
Can't a sub return an array ??
I believe it can, but makes no sense in that context because if a variable is global i can access it everytime without the need to return it from a sub.

With a return value you can use your subs as functions/procedures like this:

if ParseMyData(inputstring) = "myexpectedstring" then

or

if DoIHaveMail(value) = true then


Globals make everything work, but i prefer to avoid globals whenever i can - for several known reasons:

- if a variable gets the wrong value i have to look into every sub that's using it

- each variable can only be used one time in the entire code

- subs can't be used as functions

Certainly there are more reasons not to use globals.


The use of the return value as function would also prevent the return of more than one value...if mysub(value) ...condition..then would have more than one parameter and cannot make a decision then.

So the use of an array as return value would be the solution here...if there were not the caveat that every array has to be global in B4P.

Furthermore as far as i remember, there seem to be restrictions if the optimized compiling is used..?


cheers

TWELVE
 

specci48

Well-Known Member
Licensed User
So the use of an array as return value would be the solution here.
There is no restriction on this, because the return value is always a (single) object. So any type (even structures) will do...

if there were not the caveat that every array has to be global in B4P.
This seems to be a design restriction of basic4ppc. Even user defined structures have to be globals. :sign0161:


specci48
 

agraham

Expert
Licensed User
There is no restriction on this, because the return value is always a (single) object. So any type (even structures) will do...
I am afraid that this is not the case for the optimising compiler. Due to the way internal Subs are declared in optimised code "Return" can return only Value types i.e array elements such as "Array(2)" but not array references such as "Array()." It is possible for libraries to return Array/struct references that can be assigned to already "Dimmed" arrays but this is a special case.

I seem to be missing the point of the original question. :confused: I know of no computer language that can return more than one result from a function.
 

RandomCoder

Well-Known Member
Licensed User
The easiest way to return more than one value is to hide them in a string....
Return "true,false,26,AnythingElse?"

Then you just need to interegate the returned value using string manipulation.

Regards,
RandomCoder
 

specci48

Well-Known Member
Licensed User
I am afraid that this is not the case for the optimising compiler.
Thanks for this additional info. Haven't realized that yet. :sign0161:

I know of no computer language that can return more than one result from a function.
That's true, but in most "modern" languages the "one" result can be an object which contains several different information.
 

Erel

Administrator
Staff member
Licensed User
Currently a sub can only return one "regular" variable.
This limitation will be removed in the future (not too far), together with local arrays / structures.
 

specci48

Well-Known Member
Licensed User
Currently a sub can only return one "regular" variable.
This limitation will be removed in the future (not too far), together with local arrays / structures.
... every time one step ahead ... :sign0027:
 

klaus

Expert
Licensed User
Hi Erel,
A complementary question.
Currently the variables are transmitted to subroutines by value, could in the future the variables also be transmitted by reference ?

Best regards.
 

Erel

Administrator
Staff member
Licensed User
It will be supported in a future version too (not sure when though).
 
Top