Android Question Returning several values from Sub from a Code Module

Discussion in 'Android Questions' started by kepler, Mar 16, 2015.

  1. kepler

    kepler Active Member Licensed User

    Good morning,

    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?

    Kind regards,

  2. eurojam

    eurojam Well-Known Member Licensed User

    I use process_globals variables in the code module oder service modul. then you can use Module1.sout1...
    Sub Process_Globals
    Dim sout1, sout2 As Double
    End Sub

    Sub Function1 (sinput As Double)
      sout1 = 
    2 * sinput
      sout2 = 
    3 * sinput
    End Sub
    kepler likes this.
  3. stevel05

    stevel05 Expert Licensed User

    If you don't want to use Global variables you can return an Array, a List, a Map or a Type structure that contain as many values as you like.
    kepler likes this.
  4. Troberg

    Troberg Well-Known Member Licensed User

    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.
  5. kepler

    kepler Active Member Licensed User


    Yes, those were the solutions I was using. I thought there was another way. Just checking ;)

    Thanks anyway,

  6. DonManfred

    DonManfred Expert Licensed User

    Jut return a map or a list as result from that sub. Subs dont have an "Outvalue" given in it´s parameters.
    Sub Function1 (sinput as double) as double
    return 2 * sinput
    End Sub
    Sub Function2 (sinput as double) as double
    return 2 * sinput
    End Sub
  7. RandomCoder

    RandomCoder Well-Known Member Licensed User

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