Calling a function sub from a module

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
From what I have been able to determine, it is not possible to call a function sub from a module.
For example, if I have a Main sub that adds two variables and returns the result, from a module I cannot say:

SumXY = CallSub(Main, "AddNums(x, y)")

Instead, I have to create a global variable "SumXY" in Main then set it to the sum in the AddNums sub and in the Module just use something like:


B4X:
' Main:
Sub AddNums(x As Int, y As Int) As Int
    SumXY = x + y
End Sub

' Module:
CallSub(Main, "AddNums(x, y)")
If Main.SumXY > 10 Then ...
Is this correct, or am I missing something?
 
Last edited:

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
I think you will need to call it like this:

B4X:
' Module:
CallSub3(Main, "AddNums", x, y)

Oh, crud. Another problem to worry about. I see that to pass 3+ variables, I'll probably have to set up Types to hold them since the CallSub limit is 2. Plus, this is a huge app and the Main subs I'm calling also get called by other subs in Main as well as in the module, so I'll have to change all of those subs as well.

Plus, the question remains: is it possible to pass a value back from the Main sub to the calling Module?
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Yes it's possible, I tried this and it works fine:

B4X:
'In Code Module
Sub MyChk
   ans = CallSub3(Main, "test2", 7, 3)
   Msgbox(ans, "")
End Sub

Getting a returned value is not an issue. Passing more than 2 variables may be.

Well that's good to know. I was getting an error message on the line, but I assume now that it was because I wasn't using CallSub3 and the right format.

Thanks.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Maybe if you pass an array or a non-primitive, you can receive the modified value back as the params.

Good tip. I tried a test passing an array like this:

B4X:
' Main:
Sub AddEm(x() As Int) As Int
   Dim y As Int
   y = x(0) + x(1) + x(2) + x(3)
   x(4) = y
   Return y
End Sub

' Mod1:
Sub DoIt
   Dim x(5), y As Int
   
   For i = 0 To 3
      x(i) = i+1
   Next
   
   y = CallSub2(Main, "AddEm", x)
   Log(y & "  " & x(4))
End Sub

I was able to pass multiple values in an array and got the result back both via an element of the array and by using Return (so I would just Return).

I still have to go through all the code and change the subs and calls for every sub wanting 3+ variables, but at least the technique is simple. My new problem now is that Connect-Wireless has quit working for me, but that's a subject for another thread...
 
Last edited:
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
The strangeness continues:

I finally got the app to compile without error messages, but when I run it and come to a line like this in the Module:

B4X:
Dim x, z(3) As Int
z(0) = 1
z(1) = 2
z(2) = 3
x = CallSub2(Main, "AddEm", z)

On the CallSub2 line I get the error:
javaj.lang.NumberFormatException: Invalid double: ""

If in place of the above CallSub2 line I put:

B4X:
CallSub2(Main, "AddEm", z)
x = MySum

Where "MySum" is set to the sum in the AddEm sub, the program runs fine (except for a problem with Debug), so what the heck does that error message mean?
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
B4X:
' Main:
Sub AddEm(x() As Int) As Int
    Dim y As Int
    y = x(0) + x(1) + x(2) + x(3)
    x(4) = y
    Return y
End Sub

If your sub is still as listed above. Looks like you are trying to put the value of y into x(4) which has not been allocated in the array as a working element. You are only passing three elements to the sub AddEm and x seems to have only three elements. Try passing a fourth and fiveth element with the values of 0 or comment out the line: x(4) = y and + x(3). Like below, just my thoughts.

B4X:
' Main:
Sub AddEm(x() As Int) As Int
    Dim y As Int
    y = x(0) + x(1) + x(2) ' + x(3)
    'x(4) = y
    Return y
End Sub
 
Last edited:
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
B4X:
' Main:
Sub AddEm(x() As Int) As Int
    Dim y As Int
    y = x(0) + x(1) + x(2) + x(3)
    x(4) = y
    Return y
End Sub

If your sub is still as listed above. Looks like you are trying to put the value of y into x(4) which has not been allocated in the array as a working element. You are only passing three elements to the sub AddEm and x seems to have only three elements. Try passing a fourth and fiveth element with the values of 0 or comment out the line: x(4) = y and + x(3). Like below, just my thoughts.

B4X:
' Main:
Sub AddEm(x() As Int) As Int
    Dim y As Int
    y = x(0) + x(1) + x(2) ' + x(3)
    'x(4) = y
    Return y
End Sub

If you look back at my earlier post, you'll see that this is the same code that worked in a small test app. It just doesn't work in my huge app. Also, in the "Sub AddEm(x() as Int)" line, I'm passing it the whole array as Dimmed whether data was assigned to every element or not.

The problem seems to stem from the size of my app since the code works in a smaller app. Also, as noted in the Bugs forum, something in B4A 2.0+ seems to have caused Debug to choke on my app because of its size, which is why I was trying to move a few thousand lines into a module as Erel suggested. It didn't fix that problem, so I'm going to try to reinstall the prior version of B4A, then I won't need a module and all these problems go away.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
In your test app in the sub called doit you dimmed it with 5 elements. Now you are calling it dimmed with 3. The sub is trying to assign a value to element 5 and is trying to add element 4. This is all I was saying. It may not be the cause but seems it would generate that error.

In the post where I dimmed it to 5 (using 0-4), I passed the sum back as element 4.

In the next post, I didn't show a Dim, but I also didn't pass the value back as an element of the array. I used a Global variable, MySum.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
The strangeness continues:

I finally got the app to compile without error messages, but when I run it and come to a line like this in the Module:

B4X:
Dim x, z(3) As Int
z(0) = 1
z(1) = 2
z(2) = 3
x = CallSub2(Main, "AddEm", z)

On the CallSub2 line I get the error:
javaj.lang.NumberFormatException: Invalid double: ""

If in place of the above CallSub2 line I put:

B4X:
CallSub2(Main, "AddEm", z)
x = MySum

Where "MySum" is set to the sum in the AddEm sub, the program runs fine (except for a problem with Debug), so what the heck does that error message mean?

This is your post #8 listed above. It shows a Dim of 3. If your sub still contains the line that:


X(4) = y

That is an error. That is all I was stating. That line may no longer be in your code. I was just trying to help. :D
 
Upvote 0
Top