Qbasicnews.com

Full Version: Returning a user-defined data type from a function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Code:
dim B as vector, f as vector
f = cross_product(B,par(i).v)

function cross_product(a as vector, b as vector)
   dim c as vector
   c.x = a.y*b.z-a.z*b.y
   c.y = a.z*b.x-a.x*b.z
   c.z = a.x*b.y-a.y*b.x
   cross_product = c
end function

It tells me "Invalid data types" & points to the B. How can I do this?
When I did my vector stuff I had to make them subs with a pointer to the return answer passed to the routine. :x

Maybe this has changed with 13, or maybe I missed something.
EDIT: I guess you got it yourself... the request is gone. How did you do it?

I'd be happy too. Big Grin

Below is an example of how I did it. If you want/need I can work up the example using your problem (this is cut n paste) but this should get the point across. I don't know if this is the best way or if I'm off the mark in a major way but here it goes.

Code:
TYPE Vector2D
    X AS DOUBLE
    Y AS DOUBLE
END TYPE

SUB VectorAdd2d ( tVec as Vector2d ptr, sVec as Vector2d ptr)
   'defined such that the first one is modified
   'if you didn't want to modify either you could
   'have it pass a third one as a 'return' answer
   tVec->x = tVec->x + sVec->x
   tVec->y = tVec->y + sVec->y
END SUB


dim t as Vector2d
dim U as Vector2d

T.X = 8
T.Y = -1
U.X = 5
U.Y = 7
VectorAdd2d @U,@T
? u.x
? u.y

sleep
I got it working, thanks to your initial suggestion, but only after I posted the second post. Thanks. Big Grin I just passed the vectors byref to the sub. I wasn't smart enough to use a pointer. Maybe I'll try that later.
I had to use them (pointers) for a few different reasons when doing the vectors so I just carried it over to the subs as well.

Congrats on getting it to work.
Not allowed yet, due the complexities of the GCC 3.x ABI that FB follows.. only for prototypes.

It could be also:

Code:
function VectorAdd2d ( tVec as Vector2d, sVec as Vector2d) as Vector2d ptr
    static as Vector2d rVrec
    rVec.x = tVec.x + sVec.x
    rVec.y = tVec.y + sVec.y
    function = @rVec
end function

   v1 = *VectorAdd2d( v1, v2 )

It won't be fast as altering the parameter itself but would "simulate" what would happen if UDT's could be returned, that's it: mem copy..
Not here to reply right now, I just wanna know who that girl is on Zip's Avatar...LOL
Quote:Not here to reply right now, I just wanna know who that girl is on Zip's Avatar...LOL

It's me, of course! :bounce:
Just that she looks like someone I remember from some TV show not too far back in time...lol some series like smallville or OC type series, but not that one, older maybe 5 years before, can't remember the name lol. Old timers unite...Adosorken? what am I talking about? lol

EDITED: had my reply on for a while, didn't see that you replied yet lol. It's you huh? Hmmmm......What are you doing asking programming questions? Model FCOL!!! roflmao.
Quote:Just that she looks like someone I remember from some TV show not too far back in time...lol some series like smallville or OC type series, but not that one, older maybe 5 years before, can't remember the name lol. Old timers unite...Adosorken? what am I talking about? lol

EDITED: had my reply on for a while, didn't see that you replied yet lol. It's you huh? Hmmmm......What are you doing asking programming questions? Model FCOL!!! roflmao.

Just kidding. I did an image search on google for "hot chick" & came up with this. Tongue I guess it's just what I WISH I looked like. *sigh*
Pages: 1 2