B4J Question I'm confused by CallSub...

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

B4X:
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?
 

emexes

Expert
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?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The return is an array of byte arrays, which appears to be passed by value,
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.
 
Upvote 0

SeaBee

Member
Licensed User
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?

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?
 
Upvote 0

SeaBee

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

MarkusR

Well-Known Member
Licensed User
Longtime 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
ends.
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.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0
Top