Return value from a function

Blitz3D Forums/Blitz3D Beginners Area/Return value from a function

Valgar(Posted 2004) [#1]
Hallo to all.
I have a problem....
I'm writing a bunch of function that made some calculation (mainly for object movements....)but Blitz return only one value from a function.
I have read the book written by Krylar and at some point he tell that the function CAN return more than one value from a function with a "bit" of trickery......but he don't tell what trick he use.
Do you know what he talk about?
Because i really need to return two value from my function....
Else i don't know how to make generic function for moving object similar to blitz command (the other way is to print the commands all the time...but it a lot of time!).
Thanks


WolRon(Posted 2004) [#2]
I don't know what Krylar was talking about since I've never read his book but...

You could try storing the values in an Array (which are global). That way you could store/read info to/from the array whether you are within a function or not.

Just one idea.


eBusiness(Posted 2004) [#3]
Or just use globals:


soja(Posted 2004) [#4]
It's true that a function can only return one value, so it requires one more layer of abstraction in order to "return" multiple values. This is probably what he meant by "trickery".

The first thing I can think of is that you can return a custom type with two fields for each value. Or, if you're returning two ints that are less than 2^16, you can use bit shifting\masking techniques to pack the shorts into one int and return that (and then unpack them afterwards). You could also use a bank.

Anyway, I prefer the custom type method. No matter what you do, though, another layer of abstraction means more processing/muddling.


Valgar(Posted 2004) [#5]
Hey!I never imagine the use of globals or types!
You give me a greath idea!
Now i can modify my function to output what i want....the only back is that i can declare many global because if i use the same name when i use a movement other object move the same!
I think that i use the array mode and use global only for some function.
About the "more processing" thing....do you think that if i use this tecnique for moving object i have a decrease in performance?
Or it's only smaller performance loss?
Thanks for the hint!


eBusiness(Posted 2004) [#6]
Huh, that's certainly Italian English ;)

@ soja, don't get him rolled into that type stuff from the beginning, at least not until there is a task complicated enough to justify it.


BlackD(Posted 2004) [#7]
Blitz performs math very quickly. It can do several million calculations per second.. :) You won't take a performance hit by using a calculation as often as u want. For example:
For I = 1 To 1000000
A=12*10
Next
Print "DONE"

This should take less than half a second to complete. And changing it do a division (a slightly longer CPU process) only takes about 4 hundreths of a second longer. Thats a million divisions in half a second. You aren't going to have any problems. :)

+BlackD


BlackD(Posted 2004) [#8]
Don't get him rolled into TYPE stuff? :D I avoided using types for years over a decade and now that I figured them out I can't stop using them. So useful. :)


eBusiness(Posted 2004) [#9]
Addicted to his misuse


Valgar(Posted 2004) [#10]
Haha sorry for my italian english!
I hope to learn better english as i hope to learn better blitz (and i think that i just made another italianished typo...o_O )


CS_TBL(Posted 2004) [#11]
Reference vars.. anyone ? I keep asking for it in 'suggest ideas' topics .. :) QBasic already had this.. basicly every var in a function call was a reference, unless you put a () around it .. so: bla((a),b,c) meant that b and c were references while a is not.

this update for blitz would be cool:
x=4
y=5
z=3
convert3d(x,y,z)

end

function convert3d(&x,&y,z)
  x=x/z
  y=y/z
end function



CyberHeater(Posted 2004) [#12]
I'd prefer that the language keep to the simpler form.

From the official discription of Blitz3D on this website.

"Blitz3D provides a simple yet powerful environment for game creation - simple, because its based around the popular and easy to use BASIC programming language; and powerful, thanks to a highly optimized underlying 2D/3D engine."


CS_TBL(Posted 2004) [#13]
do you really think that a reference var makes blitz-life more miserable ??


AbbaRue(Posted 2004) [#14]
If you use an array in your function you can have it return as many values as you want.
And once the values are passed, you can use the same array over and over again.
The array can be a lot larger then you need, and then you just use as much of it as you need.
I would leave the 'Function Return' for a flag to let you know that the function worked OK.


StOrM3(Posted 2004) [#15]
hey question.. what would be the best way to return a mesh from a function ? I have a function that modifies a mesh passed in, and then I do an addmesh to put the two different meshes together, I want to be able to return the newly created mesh.. how would I specify that, that function returns a mesh ?


Tiger(Posted 2004) [#16]
Why not use a bank to return multi-values?


StOrM3(Posted 2004) [#17]
explain banks... I want to learn how to use them.. and what they are.


eBusiness(Posted 2004) [#18]
A bank is just a chunk of memory, you can write all kind of stuff. Every position is 1 byte large, so if you write an integer to position 0, it will take up position 0 to 3. Pretty much like a file.


StOrM3(Posted 2004) [#19]
okay, then could I use this to return a mesh from a function ? Like a newly combined mesh from 2 different meshes I used addmesh on.. What I did, was change teh Cel-Shaded function so that instead of parenting the entity to the other one, I made it addmesh the lines one to the shaded one, then freed the lines one, and am attempting to return the newly created mesh, can't I just use return mesh ? and if so, what identifier do I put at the end of the function name to identify the return type as a mesh ?


StOrM3(Posted 2004) [#20]
Also, would using a bank be faster than using an array ? Like instead of an array of integers, like for my grid layout info, because aren't banks supposed to be really fast ? Poking values, and Peeking values to read from them ?


big10p(Posted 2004) [#21]
Yes, just use 'return mesh' to pass the mesh handle back to the caller. You don't need to specify a return type in the function name as blitz functions return integers by default, and mesh handles are integers.


StOrM3(Posted 2004) [#22]
thanks big10p, do I denote some sinisism in your voice ? That is what I thought I was returning was a handle, but wasn't sure that was what I wanted. So the handle to that mesh will be fine for using the saveB3D functions... Okay, just wondered, if I should use the object and handle functions etc.. Sorry, my brain is fried though big10p, I only have 900+ lines of code in so far, in a total of 4 functions....

Give me a break.. even Carmack needs to ask a stupid question every now and then.


big10p(Posted 2004) [#23]

do I denote some sinisism in your voice ?



*confused* Not at all. I'm not sure why you think that, I was just trying to answer your question. ;)


Valgar(Posted 2004) [#24]
I haven't noticed any sinisism also :)
Excuse but for me most fo the people around blitz are very greath people indeed!
I have modified ALL mi routines to use multiple array to pass value out function,and works greath!
But i have noticed that if i use many complex calculation (like sin,cos,arc and similar...)into a type......well with 20 object on screen my speed come down to a crawl.....maybe i can build look-up tables??


StOrM3(Posted 2004) [#25]
I know, was just kidding, everyone on these forums are great. I have been to bad forums, meaning on 3dRad forums, and gotten no response, or smart remarks, or bad advice, or too many questions back to justify even asking a question, I love blitz3D compared to 3DRad, I have used both extensively, and Blitz3D is alot more powerful, the users are more friendly and willing to help out, without the responses I have gotten here in the forums, I might have finished my games, but it would have taken months longer. ;')

Thanks again to everyone, I really appreciate the help.