B4J Question I'm confused by CallSub...

Discussion in 'B4J Questions' started by SeaBee, Jul 29, 2019.

  1. SeaBee

    SeaBee Member Licensed User

    We have CallSub, CallSub2 and CallSub3 for no parameters, 1 parameter and 2 parameters respectively. There are examples on the forum advocating passing parameters as types, arrays and objects to avoid the parameter limit.

    I have been passing 3 or 4 parameters to a sub without any problem - because I group all my subs into modules, where there seems to be no limit. For instance:-

    Dim AllTrigArgs As Object = TrigArgsNew.ByteArgs(NameStr(I), dRows(I), dLen(I), True)
    The return is an array of byte arrays, which appears to be passed by value, but it may be by reference to multiple copies of the same-name object in the sub. I have modules for such things as numeric manipulation, string manipulation, data preparation, and so on, all containing subs with multiple input parameters.

    My concern is that, apart from being frowned on (though I cannot see why), I may be introducing some instability or unreliability into the code.

    Is my approach sound practice, or should I change my ways?
  2. emexes

    emexes Well-Known Member Licensed User

    Could you show us a matching CallSub?

    What I am slightly confused about is... if you can directly call the function ByteArgs() in the module TrigArgsNew, then can you not also directly call any other function in that same module TrigArgsNew, without needing to package up parameters and use CallSub?

    Or is the function that you are CallSub'ing in a different module/activity?
  3. Erel

    Erel Administrator Staff Member Licensed User

    When you return an array from a sub it is not copied. You return a reference to the same array (the reference is passed by value but it is less important).

    I don't see any problem with your code, assuming that the indirect calling is indeed required.
  4. SeaBee

    SeaBee Member Licensed User

    In my normal methodology I have Main, which handles the UI, and acts as a clearing house for lots of modules which each contain multiple subs, most of which have multiple parameters. I keep all program logic outside Main, which is pretty easy as one module sub can call another module sub directly, again with multiple parameters. I have been working this way with B4X for the last three years - when I first started using it for porting a lot of VB.NET code across for Android.

    I have never used CallSub in any form, as I have never needed to - and didn't even know it existed until quite recently. This is the essence of my query. I have no formal background in programming, as you know, so I never knew any better!

    Is there anything wrong with my approach? If it comes down to it, why does CallSub exist, except of course for CallSubDelayed?
  5. SeaBee

    SeaBee Member Licensed User

    Thanks or that. The reason I raised the issue is that the Sub AllTrigArgs.ByteArgs is run many times in succession by a For... Next loop, and each run result is put into a specifically named object in the calling program, so there are multiple copies of the original with the same name lurking around somewhere.
  6. Erel

    Erel Administrator Staff Member Licensed User

    The fact that you call it with CallSub doesn't matter. It behaves the same as calling the sub directly.
    MarkusR likes this.
  7. MarkusR

    MarkusR Well-Known Member Licensed User

    if u assign a new object into a variable the garbage collector clean up memory for unused objects.
    and typically if u declared a variable in the function (sub or method) scope, this variables are deleted after the sub
    problematic are global variables or lists if you store a reference there,
    you need to null the variable and need remove the obj. reference from list to clean um memory.

    better than code modules are the use of classes.
  8. OliverA

    OliverA Expert Licensed User

  9. Erel

    Erel Administrator Staff Member Licensed User

    1. Duck typing.
    2. Dynamic calling.
    3. Raising events.
    OliverA likes this.
  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