Passing arrays to Subs.

jesb4ppc

Member
Licensed User
Hi everyone,

I've been checking how to pass arrays as parameters to Subs. As I got some errors, try to use the sample in help:
Sub Globals
Dim arr1(10), arr2(10)
Dim tempArray (0)
End Sub


Sub App_Start
SomeSub (arr1())
msgbox(arr1(5)) 'will show 5
End Sub


Sub SomeSub (tempArray())
tempArray(5) = 5
End Sub


But got the same error. (ErrorArrays.jpg). If I comment the Dim TempArray(0) in Globals, got another error (ErrorArrays2.jpg)

So, my question is how do I do to pass arrays to Subs?:sign0085:

Thanks in advance,

Jes.
 

Attachments

  • ErrorArrays.JPG
    ErrorArrays.JPG
    11 KB · Views: 211

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can't pass the array as a parameter.
However you can assign one array to another and get the same result:
B4X:
[SIZE=3][COLOR=Black][SIZE=2]Sub Globals
 Dim arr1(10), arr2(10)
 Dim tempArray (0)
End Sub


Sub App_Start
tempArray() = arr1()
 SomeSub
 msgbox(arr1(5)) 'will show 5
tempArray() = arr2()
SomeSub

End Sub


Sub SomeSub 
  tempArray(5) = 5
End Sub
[/SIZE][/COLOR][/SIZE]
 

jesb4ppc

Member
Licensed User
OK.

Ok. But as I took the sample from Help, maybe the help file should be updated, just for not other users to go wrong.

Thank you, Erel.
 

jesb4ppc

Member
Licensed User
It's in Help --> Main Help-->Find-->Variables.
I copy&paste it for you to see.

All simple variables are variant variables.
Which means that any variable can store any type of number or a string.
Variables can be global or local.
Local variables value can be used only while their parent sub is executing.
Global variables’ values can be used everywhere.
Except for array and structure variables, there is no need to declare a local variable before using it.
Since version 5.0 an optional (and recommended) check is done to make sure that all local variables are used and that no variable is used before it is assigned any value.
Global variables are variables that were declared (or used) in Sub Globals; all other variables are local.


Example:
(An error message will show if using the check unassigned variables.)
Sub Globals
a=20
End Sub


Sub App_Start
b=10
CalcVars
End Sub


Sub CalcVars
Msgbox ("a = " & a)
Msgbox ("b = " & b)
End Sub


Result: First msgbox will show a = 20
Second msgbox will show b =
b is empty because it's local and wasn't assigned any value yet in this sub.


To pass data between subs you could use global variables, or better, you can pass the data as parameters.


Example:
Sub Globals
a=20
End Sub


Sub App_Start
c=10
CalcVars (c)
End Sub


Sub CalcVars (b)
Msgbox ("a = " & a)
Msgbox ("b = " & b)
End Sub
Result: The second msgbox will show b = 10


Note: When using parameters the variable is not passed, but its value is copied.
In the above example, I used a variable named c instead of b.
Remember that if you change a local variable’s value in one place (even if it had received its data from a parameter), then the change will not affect any other local or global variable outside the current sub.




Array Variables


Basic4ppc supports arrays of up to three dimensions.
Array variables are always global and must be declared before used.
Arrays can store items of a certain data type.
This is useful for working with external libraries which sometimes expect an array of a certain type.
Declare global variables with the Dim keyword.
The same array can be declared again with a different size any number of times during the program.
However, it must be first declared in Sub Globals (even with 0 items).
An array index starts from 0 and its last index is array dimensions - 1.
Example:
Sub Globals
Dim Books (20)
Dim Buffer (100) As Byte
End Sub


Result: Declares an array of 20 items starting from Books(0) and up to Books(19) and an array of bytes starting from Buffer(0) and up to Buffer(99)
When you need to reference an entire array (not one item in the array) write the array's name followed by ().
Example:
i = ArrayLen (buffer() )
BinaryFile.WriteBytes (buffer())


Arrays can be declared with 0 items like:
Dim Buffer(0) As Byte
Buffer is an empty array that can be used later with an external library which returns an array.
Example:
Buffer() = Serial.InputArray


Arrays can be passed as parameters to other subs.
Unlike other variables, arrays are passed by reference.
Changing the value of the array in the target sub will also change the array in the caller sub.
Example:
Sub Globals
Dim arr1(10), arr2(10)
Dim tempArray (0)
End Sub


Sub App_Start
SomeSub (arr1())
msgbox(arr1(5)) 'will show 5
End Sub


Sub SomeSub (tempArray())
tempArray(5) = 5
End Sub


Structure Variables


Structures are variables with customized fields.
Using structures the code can be clearer and better organized.
Structures must be declared in Sub Globals.
See the Dim keyword for information on declaring structures.
Using regular structures is similar to using a control's properties.
Write the name of the variable followed by a period '.' and a list of the available fields will pop.
Example:
Sub Globals
Dim Type(Name, ID, Age) person
End Sub


Sub App_Start
person.Name = "John"
person.ID = 1234567
person.Age = 30
End Sub


You could also use arrays of structures (up to two dimensions):
Sub Globals
Dim Type(Name, ID, Age) persons (100)
End Sub


Sub App_Start
persons(0).Name = "John"
persons(0).ID = 1234567
persons(0).Age = 30
End Sub


Notes:
- On the device only, you need to press on Subs - REFRESH in order that any declaration change will appear in the pop up list.
- You can reference the whole structure using the structure name followed by ().
Example: person() = SomeSub
- Structures are converted to arrays during the compilation.
In the first example, person will be an array of one dimension, person.Name = "John" will be converted to person (0) = "John" and so on.
You can also use the array syntax to work with structures.
 

jesb4ppc

Member
Licensed User
That's true. I got help v5.80.

But if it was possible to pass arrays like arguments in prior versions, I think it would be quite useful to do it in new versions also.

Thank you again for your support.

Jes.
 

mjcoon

Well-Known Member
Licensed User
That's true. I got help v5.80.

But if it was possible to pass arrays like arguments in prior versions, I think it would be quite useful to do it in new versions also.

Thank you again for your support.

Jes.

Agreed! I have just discovered this thread after diligently trying to discover how to pass array parameters since it seems such an obvious feature to have.

Needing to have a globally-declared "parameter array" and assigning that before each call is very clunky and not good "encapsulation".

Mike.
 

mjcoon

Well-Known Member
Licensed User
Oh, yes, and the Help says: 'If there are no parameters for the sub, then write "Sub SubName" without the parenthesis.'

But if I try this I get told I have omitted the parenthesis, so the compiler wins!

Mike.
 

mjcoon

Well-Known Member
Licensed User
Calling without parentheses refers to Subs declared without a parameter list. I suspect you are trying to call a Sub that needs one or more parameters without the parentheses in which case the compiler will complain.

Hi Andrew, I assure you that your suspicions are unfounded. However further experimentation shows that my quote from the help apparently only applies to Subs that are declared without any return value typing. Since I like strong typing (I started with Algol-60 back in the 60s!) I like to use typing if the Sub is meant to return a value. Then I find that I have to put "()" in the declaration even if the Sub has no parameters.

This sort of stuff reminds me that although Algol-60 had just as much of a problem with defining the semantics as any other language, at least its syntax was tightly specified, so that syntactic units could be referred to unambiguously.

Regards, Mike.
 

agraham

Expert
Licensed User
Longtime User
Since I like strong typing
So do I. One of the best things to happen to C was the introduction of strong typing in Borland and ANSI C - and it's one of the reasons I love .NET. But as Basic4ppc is untyped I don't understand what you mean at all - though I'm probably being stupid :sign0161: and missing the obvious.

I like to use typing if the Sub is meant to return a value. Then I find that I have to put "()" in the declaration even if the Sub has no parameters.
:confused:
 

mjcoon

Well-Known Member
Licensed User
... But as Basic4ppc is untyped I don't understand what you mean at all - though I'm probably being stupid :sign0161: and missing the obvious.
:confused:

No, no, it's certainly not your stupidity, it's my inability to conform combined with Basic4ppc's acquiescence that caught me.

Following on from my use of Visual Basic at work, I had been declaring Subs with typing, and Basic4ppc had been indulging me, though perhaps just ignoring the types I specified. That was when I found that if I omitted the "()" to signify no parameter list that Basic4ppc started complaining.

In the interim I have now got my program working transferring binary data over serial RS232. So thank you very much for your work on sorting out the 0x1A problem, for providing SerialEx.dll and for your assistance to me. The BytesConverter.dll was crucial in interpreting the numerics embedded in the (Garmin GPS) binary stream, too.

Best wishes, Mike.
 
Top