Can a sub return more than 1 value ?

Discussion in 'Questions (Windows Mobile)' started by TWELVE, May 11, 2008.


    TWELVE Active Member Licensed User


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

    Return value

    can only transport one value, right..?


  2. Cableguy

    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....
  3. taximania

    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.
  4. Cableguy

    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

    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


    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..?


  6. specci48

    specci48 Well-Known Member Licensed User

    There is no restriction on this, because the return value is always a (single) object. So any type (even structures) will do...

    This seems to be a design restriction of basic4ppc. Even user defined structures have to be globals. :sign0161:

  7. agraham

    agraham Expert Licensed User

    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.
  8. RandomCoder

    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.

  9. specci48

    specci48 Well-Known Member Licensed User

    Thanks for this additional info. Haven't realized that yet. :sign0161:

    That's true, but in most "modern" languages the "one" result can be an object which contains several different information.
  10. Erel

    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.
  11. specci48

    specci48 Well-Known Member Licensed User

    ... every time one step ahead ... :sign0027:
  12. TWELVE

    TWELVE Active Member Licensed User

    Thanks gentlemen for your contribution to this...:)


  13. klaus

    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.
  14. Erel

    Erel Administrator Staff Member Licensed User

    It will be supported in a future version too (not sure when though).
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice