Cant compile Sub with vector parameters
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
Cant compile Sub with vector parameters
Hi!
Here my first programming question of, I´m afraid, a lot to come
I want to pass a "vector" to a subroutine and I do not know how to do it.
Here the options I tried.
Just works the Option 3, but is useless to use a vector if i need to pass each component of the vector one by one
Any hint?
Thanks!
Here my first programming question of, I´m afraid, a lot to come
I want to pass a "vector" to a subroutine and I do not know how to do it.
Here the options I tried.
Just works the Option 3, but is useless to use a vector if i need to pass each component of the vector one by one
Any hint?
Thanks!
Re: Cant compile Sub with vector parameters
Code: Select all
Type Vector
x As Integer
y As Integer
z As Integer
End Type
Sub PrintVector(Byref v As vector)
Print "(" & v.x & ", " & v.y & ", " & v.z & ")"
End Sub
Dim v As Vector = (1, 2, 3)
PrintVector(v)
Sleep
Re: Cant compile Sub with vector parameters
fxm beats me
Another way to do that.
Another way to do that.
Code: Select all
sub test(v() as integer)
for iv as integer =lbound(v) to ubound(v)
print iv,v(iv)
Next
end sub
dim as integer v1(1 to 10)={1,2,3,4,5,6,7,8,9,100}
test(v1())
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
Re: Cant compile Sub with vector parameters
Thanks guys! As fast as always!
Re: Cant compile Sub with vector parameters
To avoid getting random result (without an error message) if the provided array has more than one dimension:
It is preferable to impose an array of dimension 1 in the function call parameter:
Code: Select all
sub test(v() as integer)
for iv as integer =lbound(v) to ubound(v)
print iv,v(iv)
Next
end sub
dim as integer v1(1 to 10, 1 to 2)
test(v1())
It is preferable to impose an array of dimension 1 in the function call parameter:
Code: Select all
sub test(v(any) as integer)
for iv as integer =lbound(v) to ubound(v)
print iv,v(iv)
Next
end sub
Re: Cant compile Sub with vector parameters
So not anymore a vector but a matrix
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
Re: Cant compile Sub with vector parameters
Mmm, sorry. Or I do not understand or is not what I need.fxm wrote: ↑Dec 31, 2023 13:51Code: Select all
Type Vector x As Integer y As Integer z As Integer End Type Sub PrintVector(Byref v As vector) Print "(" & v.x & ", " & v.y & ", " & v.z & ")" End Sub Dim v As Vector = (1, 2, 3) PrintVector(v) Sleep
I need a thing that I could use like this:
Code: Select all
Dim Vector (1 to 5) as integer
dim as integer i
Vector(1)=17
Vector(2)=21
Vector(3)=-10
Vector(4)=104
Vector(5)=12345
for i=1 to 5
print Vector(i)
next i
sleep
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
Re: Cant compile Sub with vector parameters
In other words:
How to pass an array to a subroutine that calculate its components?
How to pass an array to a subroutine that calculate its components?
Re: Cant compile Sub with vector parameters
As SARG showed.
Because an array is passed to a procedure by reference, the procedure body can also modify the elements of the passed array.
Because an array is passed to a procedure by reference, the procedure body can also modify the elements of the passed array.
Code: Select all
Sub InitArray(array() As Integer)
array(1)=17
array(2)=21
array(3)=-10
array(4)=104
array(5)=12345
End Sub
Dim Vector (1 to 5) as integer
dim as integer i
InitArray(Vector())
for i=1 to 5
print Vector(i)
next i
sleep
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
Re: Cant compile Sub with vector parameters
Works nice! Thanks!fxm wrote: ↑Dec 31, 2023 21:36 As SARG showed.
Because an array is passed to a procedure by reference, the procedure body can also modify the elements of the passed array.
Code: Select all
Sub InitArray(array() As Integer) array(1)=17 array(2)=21 array(3)=-10 array(4)=104 array(5)=12345 End Sub Dim Vector (1 to 5) as integer dim as integer i InitArray(Vector()) for i=1 to 5 print Vector(i) next i sleep