A acorrias Member Licensed User Longtime User Oct 8, 2011 #1 Hi I'm discovering b4a languages features in order to get a large know how. I have two questions: a) is it possibile to pass an whole array to a sub? thats to say dim a(9) as int . sub grind_array (x....) <<<<??? how to specify formal parameter??? x(5)=4 end sub I have tryed to code the following code but I get an error "Use of undeclared array: x" Code: Copy Sub Globals Dim b(10), i As Int For i=0 To 9 : b(i)=i*i : Next Log("actual param before: "& b(5)) 'Prints b(5) before sub call S2(b) Log("actual param after: " & b(5)) 'Prints b(5) after sub call End Sub Sub S2(x As Object) Log("formal param before: "& x) 'Prints x x(5)=666 Log("formal param after: " &x) 'Prints x End Sub b) I read the Erel's post about Variables & Objects in Basic4android. I'm studying about the phrase "When you pass a non-primitive to a sub or when you assign it to a different variable, a copy of the reference is passed." and about "All types can be treated as Objects". So I tryed another example passing the single array cell but it seems that it's behaving as value passing. let's see. Code: Copy Sub Globals Dim b(10), i As Int For i=0 To 9 : b(i)=i*i : Next Log("actual param before: "& b(5)) 'Prints b(5) before sub call S2(b(5)) Log("actual param after: " & b(5)) 'Prints b(5) after sub call End Sub Sub S2(x As Int) Log("formal param before: "& x) 'Prints x x=666 Log("formal param after: " & x) 'Prints x End Sub I thought that the correct value for the actual param after the s2 call would be 666 and not 25. So, could you help me understanding this topic. cheers alex
Hi I'm discovering b4a languages features in order to get a large know how. I have two questions: a) is it possibile to pass an whole array to a sub? thats to say dim a(9) as int . sub grind_array (x....) <<<<??? how to specify formal parameter??? x(5)=4 end sub I have tryed to code the following code but I get an error "Use of undeclared array: x" Code: Copy Sub Globals Dim b(10), i As Int For i=0 To 9 : b(i)=i*i : Next Log("actual param before: "& b(5)) 'Prints b(5) before sub call S2(b) Log("actual param after: " & b(5)) 'Prints b(5) after sub call End Sub Sub S2(x As Object) Log("formal param before: "& x) 'Prints x x(5)=666 Log("formal param after: " &x) 'Prints x End Sub b) I read the Erel's post about Variables & Objects in Basic4android. I'm studying about the phrase "When you pass a non-primitive to a sub or when you assign it to a different variable, a copy of the reference is passed." and about "All types can be treated as Objects". So I tryed another example passing the single array cell but it seems that it's behaving as value passing. let's see. Code: Copy Sub Globals Dim b(10), i As Int For i=0 To 9 : b(i)=i*i : Next Log("actual param before: "& b(5)) 'Prints b(5) before sub call S2(b(5)) Log("actual param after: " & b(5)) 'Prints b(5) after sub call End Sub Sub S2(x As Int) Log("formal param before: "& x) 'Prints x x=666 Log("formal param after: " & x) 'Prints x End Sub I thought that the correct value for the actual param after the s2 call would be 666 and not 25. So, could you help me understanding this topic. cheers alex
Erel B4X founder Staff member Licensed User Longtime User Oct 8, 2011 #2 a) Yes. Code: Copy Dim x(10) As Int Dim y(20, 30) As Int Dim z() As Button z = s1 (x, y) Sub s1 (x() As Int, y(,) As Int) As Button() For i = 0 To x.Length - 1 Log(x(i)) Next ... End Sub b) The type of a specific element in your array is Int, which is a primitive type. The element is passed by value in this case. Try this code: Code: Copy Dim b(1) As Int b(0) = 25 S1(b) Log(b(0)) '666 ... Sub S1 (x() As Int) x(0) = 666 End Sub Upvote 0
a) Yes. Code: Copy Dim x(10) As Int Dim y(20, 30) As Int Dim z() As Button z = s1 (x, y) Sub s1 (x() As Int, y(,) As Int) As Button() For i = 0 To x.Length - 1 Log(x(i)) Next ... End Sub b) The type of a specific element in your array is Int, which is a primitive type. The element is passed by value in this case. Try this code: Code: Copy Dim b(1) As Int b(0) = 25 S1(b) Log(b(0)) '666 ... Sub S1 (x() As Int) x(0) = 666 End Sub