Is this a bug?

Blitz3D Forums/Blitz3D Beginners Area/Is this a bug?

-=Darkheart=-(Posted 2003) [#1]
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


skidracer(Posted 2003) [#2]
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.


-=Darkheart=-(Posted 2003) [#3]
Duh, I get it now, thanks Simon.

Darkheart


BlitzSupport(Posted 2003) [#4]
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



Oldefoxx(Posted 2003) [#5]
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.


MuffinRemnant(Posted 2003) [#6]
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?? :)


ashmantle(Posted 2003) [#7]
this is a bug!
[img http://images.google.com/images?q=tbn:0_sroo-LgeIC:geology.asu.edu/geoclub/scenery/bug.jpg[/img]


ashmantle(Posted 2003) [#8]
oops.. double posts :(