stupid question about return

Blitz3D Forums/Blitz3D Beginners Area/stupid question about return

Tau_Aquilae(Posted 2011) [#1]
Is it possible to return a type? or just values?


GfK(Posted 2011) [#2]
You can return anything, but you need to specify the returned type in the function declaration. If you need sample code, somebody else will have to help with that as I haven't used BB3D for years now.


Tau_Aquilae(Posted 2011) [#3]
ok so something like this?

Function create_unit (unit)

borg_cube.unit=new unit

Federation_starship.unit=new unit

return borg_cube.unit ;only borg_cube.unit will be returned

end function




GfK(Posted 2011) [#4]
Assuming you only want the unit object returned (and not borg_cube):
Function create_unit:unit ()

borg_cube.unit=new unit

Federation_starship.unit=new unit

return borg_cube.unit ;only borg_cube.unit will be returned

end function

I *think* that's right. It might be Function create_unit.unit() - I can't remember!


Tau_Aquilae(Posted 2011) [#5]
ok I'll experiment more...thanks!

Last edited 2011


Yasha(Posted 2011) [#6]
Blitz Classic syntax uses a dot, not a colon, for marking variable types. Otherwise, the example's right.

What you can't do is the version given in post #3: types aren't a first-class value, so you can't pass the type to instantiate as a parameter (the best you could do is pass an integer constant and have a Select structure create the thing you want then return an integer Handle... i.e. a mess). Same way you wouldn't attempt to pass in "#" or "%" - ".unit" is exactly the same concept as either of those.

Also, a rant about correct terminology: http://www.blitzbasic.com/Community/posts.php?topic=94874#1090218 (getting the words right in programming is extremely important! Pushing words around and making them work is what we do...)

Last edited 2011


Tau_Aquilae(Posted 2011) [#7]
ok so instead of


 

function create_unit (unit)



I should do


function create_unit.unit ()



yes?


Yasha(Posted 2011) [#8]
Yes.


Tau_Aquilae(Posted 2011) [#9]
ok I think i got the hang of it thanks!


Kryzon(Posted 2011) [#10]
You state as a suffix to the function's name the type of value the function will return:
$ - For string values.
# - For float values.
% - For integer values.
.name - For custom objects declared with "Type".

If you want to pass custom objects as arguments, using the logic above a function would be named like this:
Function CreateExplosion.TExplosion( Location.TLocation )
	Exp = New TExplosion
	Exp\size = Rand(5,15)
	Exp.X = Location\z ;Using fields of the object that was passed to this function.
	Exp.y = Location\y
	Exp.z = Location\z

	Return Exp
End Function
This function requires as an argument an instance of a TLocation object. Inside this function you can access this instance's fields, Delete() it or do whatever you want.

That function also returns an instance of a TExplosion object, so I need to put a ".TExplosion" in front of the function's name.
If I were returning a string, the function's name would be "CreateExplosion$".

@Yasha: what should you call those variables you place inside the brackets, such as "Function( bla = 0 )". The 'bla' would be called argument or parameter?

Last edited 2011


Yasha(Posted 2011) [#11]
@Yasha: what should you call those variables you place inside the brackets, such as "Function( bla = 0 )". The 'bla' would be called argument or parameter?


If y'all want to get technical...

"bla" is the "formal parameter", or just "parameter". If you then call the function, e.g. "foo(6)", then "6" is the "actual parameter" or "argument".

In practice, either term will usually be understood as it's pretty rare for there to be any reason to draw a distinction, especially in Blitz.


Kryzon(Posted 2011) [#12]
That's great, thanks. Just for the sake of knowledge :)