Possible bug with 'return;'.
by Jon Wilsdon · in Torque Game Engine Advanced · 06/25/2007 (3:14 pm) · 4 replies
I am not sure if this is a bug, or if it is desired behavior, and a quick look around using the Garage Games search didn't lead me to any previous forum posts about it. I imagine this behaves the same way in TGE, but I don't have the time to check.
If you run that function with all 3 cases, your return values will be:
Returning whatever is in the string buffer is not the behavior I expected when writing 'return;'.
function returnSomeString( %case ) {
%tempVar = "hey!";
if( %case == 0 ) {
return;
}
if( %case == 1 ) {
return "";
}
if( %case == 2 ) {
return "SomeString";
}
}If you run that function with all 3 cases, your return values will be:
==> echo( returnSomeString( 0 ) ); hey! ==> echo( returnSomeString( 1 ) ); ==> echo( returnSomeString( 2 ) ); SomeString
Returning whatever is in the string buffer is not the behavior I expected when writing 'return;'.
#2
06/25/2007 (3:27 pm)
Perl and some versions of smalltalk do this. I'm sure there are other languages/compilers/interpreters that also exhibit this behavior. It is a "known" feature of TorqueScript. I've seen it written somewhere in docs.
#3
The behavior is unexpected for me because it would seem to complicate the program flow for little gain. However, if nothing else, maybe this post will make finding the many uses of 'return' easier to find in a search, or remind people who might have forgotten about it.
Thanks for the quick replies Thomas and Ben.
06/25/2007 (3:41 pm)
I suspected it was known, but searching for it was a pain since clearly searching for 'return' isn't useful. :)The behavior is unexpected for me because it would seem to complicate the program flow for little gain. However, if nothing else, maybe this post will make finding the many uses of 'return' easier to find in a search, or remind people who might have forgotten about it.
Thanks for the quick replies Thomas and Ben.
#4
On the other hand, if the function is one-way only and isn't meant to return a result, don't do it (use only 'return').
Do not mix those, it's bad programming practice which is actually impossible to do on strongly-typed languages (the compilers yell at you).
06/26/2007 (8:25 am)
Rule of thumb: if your function is supposed to return a result, you must always return something. On the other hand, if the function is one-way only and isn't meant to return a result, don't do it (use only 'return').
Do not mix those, it's bad programming practice which is actually impossible to do on strongly-typed languages (the compilers yell at you).
Torque Owner Thomas Petersen