Cant compile Sub with vector parameters

New to FreeBASIC? Post your questions here.
Post Reply
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Cant compile Sub with vector parameters

Post by Luis Babboni »

Hi!

Here my first programming question of, I´m afraid, a lot to come :oops:

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 :?

Image

Any hint?
Thanks!
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Cant compile Sub with vector parameters

Post by fxm »

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
SARG
Posts: 1768
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Cant compile Sub with vector parameters

Post by SARG »

fxm beats me :D
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())
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Cant compile Sub with vector parameters

Post by Luis Babboni »

Thanks guys! As fast as always! :-)
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Cant compile Sub with vector parameters

Post by fxm »

To avoid getting random result (without an error message) if the provided array has more than one dimension:

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
SARG
Posts: 1768
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Cant compile Sub with vector parameters

Post by SARG »

So not anymore a vector but a matrix :lol:
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Cant compile Sub with vector parameters

Post by Luis Babboni »

fxm wrote: Dec 31, 2023 13:51

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
Mmm, sorry. Or I do not understand or is not what I need.

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
But that what calculates the Vector components is a subroutine.
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Cant compile Sub with vector parameters

Post by Luis Babboni »

In other words:

How to pass an array to a subroutine that calculate its components?
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Cant compile Sub with vector parameters

Post by fxm »

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
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Cant compile Sub with vector parameters

Post by Luis Babboni »

Uff, I made the same question, 3 years ago!! :-(

viewtopic.php?t=29134

Let me see. Sorry.
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Cant compile Sub with vector parameters

Post by Luis Babboni »

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
Works nice! Thanks!
Post Reply