Subs parameters

merli

Member
Licensed User
Hello

I would like to have some way to return from Sub more then one value.
For example:

Sub min_max(a,b,min,max)
If a<b Then
min=a
max=b
Else
min=b
max=a
End If
End Sub

Calling as:
min_max(no1,no2,minimum,maximum)

I would expect after run filled out variables minimum and maximum with right values.

Thnx
 

derez

Expert
Licensed User
Longtime User
Your wish can be answered in two options :
1. to define min and max as global, then they are changed by the sub and you can refer to their value outside of the sub (and they should not be in the arguments list).
2. to return one variable which is a string : min & "," & max, and then use an array of 2 to split it
B4X:
Dim mix(2)

sub min_max(a,b)
...
...
return min & "," & max
end sub

mix() = strsplit(min_max(a,b) , ",")
min = mix(0)
max = mix(1)
 
Last edited:

klaus

Expert
Licensed User
Longtime User

merli

Member
Licensed User
Your wish can be answered in two options :
1. to define min and max as global, then they are changed by the sub and you can refer to their value outside of the sub (and they should not be in the arguments list).
2. to return one variable which is a string : min & "," & max, and then use an array of 2 to split it
B4X:
Dim mix(2)

sub min_max(a,b)
...
...
return min & "," & max
end sub

mix() = strsplit(min_max(a,b) , ",")
min = mix(0)
max = mix(1)

Nice workaround, :) but you still have to have arrays global declared. Erel, is it planned to have posibility declare arrays localy?
 

mjcoon

Well-Known Member
Licensed User
Nice workaround, :) but you still have to have arrays global declared. Erel, is it planned to have posibility declare arrays locally?

How would that work, except by having arrays (or other variables) passed by reference so that the names are local to the Sub but the data that is manipulated is external to the Sub? (Which is what we are promised for a future version.)

Using globals is so clunky because to operate on varied data each has to be loaded into the global(s) before calling the Sub and then "unloaded" afterwards. This doesn't help with the readability of the code.

Mike.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Nice workaround,
smile.gif
but you still have to have arrays global declared. Erel, is it planned to have posibility declare arrays localy?
progress.gif
Declaring local arrays and structures was initially planned for the next version. Though I believe it will only be added in the following version.
 

merli

Member
Licensed User
How would that work, except by having arrays (or other variables) passed by reference so that the names are local to the Sub but the data that is manipulated is external to the Sub? (Which is what we are promised for a future version.)

Using globals is so clunky because to operate on varied data each has to be loaded into the global(s) before calling the Sub and then "unloaded" afterwards. This doesn't help with the readability of the code.

Mike.

You are right, but there is another problem when I want to use in Sub some helping temporary array which won't be used after return. I still have to now define this array Global
 

mjcoon

Well-Known Member
Licensed User
... when I want to use in Sub some helping temporary array which won't be used after return. I still have to now define this array Global

Yes, the relaxed implicit-variant treatment of local variables does not work for arrays. This is because all elements of the array have to have the same variant for the indexing mechanism to work (I suppose), so the easy way out is to require all arrays to be declared in a "Dim" before they are used.

Erel has mentioned making variables more sophisticated in the past, so perhaps including a local "Dim" could be introduced for local arrays.

Mike.
 

agraham

Expert
Licensed User
Longtime User
Yes, the relaxed implicit-variant treatment of local variables does not work for arrays. This is because all elements of the array have to have the same variant for the indexing mechanism to work
You are making it sound much more complicated than it really is, it is really very simple. Normal variables, both global and local are just string variables. Arrays of the type they are declared as or strings if no type is specified. Numeric operations are always performed on Doubles and variables and arrays are coerced to and from Doubles when required for arithmetic operations. Boolean values are represented as the strings "true" and "false".
 

merli

Member
Licensed User
You are making it sound much more complicated than it really is, it is really very simple. Normal variables, both global and local are just string variables. Arrays of the type they are declared as or strings if no type is specified. Numeric operations are always performed on Doubles and variables and arrays are coerced to and from Doubles when required for arithmetic operations. Boolean values are represented as the strings "true" and "false".

Do I understand it right that all the time I am using variable in program, Basic converts this variable from string "12345678.9876543" to value? If yes isn't it much time and processor consuming?
 

agraham

Expert
Licensed User
Longtime User
Yes you understand it right and yes it takes up more CPU cycles during mathematical operations compared to using strongly typed variables but that is the price you pay for the simplicity and ease of use of a weakly typed language and in most applications, even on a device, this is usually not a problem as there are other things that dominate execution time - like the user for many interactive applications.

If you really need to do intensive mathematics in Basic4ppc you can declare one or more arrays or structs as Double and use their elements for numeric calculations. As all numeric operations are done as Doubles this avoids converting to and from string values. In an optimised compiled app this will give you a good speed boost over normal string based non-array variables. To avoid the overhead of the array access for each variable fetch and use non-array variables you could use my http://www.b4x.com/forum/additional...ecompiler-performance-enhancer.html#post28926 library.

I believe Erel intends to introduce some form of type declaration for normal variables in some future release of Basic4ppc.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I believe Erel intends to introduce some form of type declaration for normal variables in some future release of Basic4ppc.

Next version will include it.
You will be able to declare the type of all variables (including sub parameters).
The main types will be Number which is mapped to .Net Double, Integer which is mapped to Int32 and String (or undeclared).
 
Top