Is this a bug?
Blitz3D Forums/Blitz3D Beginners Area/Is this a bug?
| ||
For some reason I cannot return a string from a function, am I doing something wrong or is there some good reason why you can't do this... Example code: Graphics 800,600,16,2 SetBuffer BackBuffer() CAT$="CAT" Function MAKEDOG(C$) C$="DOG" Return C$ End Function E$=MAKEDOG(CAT$) Print E$ Darkheart |
| ||
from here: http://www.blitzbasic.co.nz/b3ddocs/command.php?name=function&ref=goto You can pass variables into functions, as well as RETURN values back to the calling code. To return floats or strings from a function ensure your function name has a # or $ suffix. |
| ||
Duh, I get it now, thanks Simon. Darkheart |
| ||
Don't forget custom types, eg.Type Cow Field blah End Type Function GimmeCow.Cow (xxx) c.Cow = New Cow c\blah = xxx Return c End Function test.Cow = GimmeCow (123) Print test\blah |
| ||
Acyually, this is an excellent point to bring up. The ability of Subs and Functions to pas changed parameter values and pass them back can be a good way to overcome the limitations of Functions to return just a single value. But it may also mean that you should reassign values passed as parameters to local variables before playing with them if you don't want them changed accidently. Furthermore, and this may or may not apply to Blitz, but does to some other compilers, local variables can sometimes be accessed faster than passed ones, so there might be some small advantage when repeatedly calling a local variable instead of continuing to use the passed variable. It depends upon how the compiler defines local variables. It is interesting to also note that if you pass a constant, such as "Example", or an expression, such as 1+2*3, that the compiler does not return a value that overwrites it. If it did otherwise, it would surely corrupt your program. So the compiler has to be somewhat clever as to when a parameter's value can be returned or not. This was a good test of one such case. |
| ||
Oldefoxx: "It is interesting to also note that if you pass a constant, such as "Example", or an expression, such as 1+2*3, that the compiler does not return a value that overwrites it." Er, could that be because it's a constant?? :) |
| ||
this is a bug! [img http://images.google.com/images?q=tbn:0_sroo-LgeIC:geology.asu.edu/geoclub/scenery/bug.jpg[/img] |
| ||
oops.. double posts :( |