GLBasic forum

Main forum => GLBasic - en => Topic started by: Alex_R on 2015-Aug-06

Title: Unexpected behavior from a function
Post by: Alex_R on 2015-Aug-06
Yesterday I had an odd behavior from a very easy function. I wanted to print the word HELLO but instead it returns always 0 value.

Code (glbasic) Select
LOCAL A$=PUTATEXT()
PRINT A$

A$=PUTATEXT2()
PRINT A$

A$=PUTATEXT3("HELLO")
PRINT A$

SHOWSCREEN
MOUSEWAIT

FUNCTION PUTATEXT:
RETURN "HELLO"
ENDFUNCTION

FUNCTION PUTATEXT2:
LOCAL B$="HELLO2"
RETURN B$
ENDFUNCTION

FUNCTION PUTATEXT3: C$
INC C$," MY FRIEND"
RETURN C$
ENDFUNCTION


Output in every case: 0

Is my GLB corrupt?
Title: Re: Unexpected behavior from a function
Post by: mentalthink on 2015-Aug-06
Alex_R try to do this:

A$=PUTATEXT$() take a look the function have in the end of the name the Symbol of the Dollar $ , I think you get 0, because the function wants return an integer not a string.

For my works ,else I get a 0 too.


LOCAL A$=PUTATEXT$()
PRINT A$

//A$=PUTATEXT2()
//PRINT A$

//A$=PUTATEXT3("HELLO")
//PRINT A$

SHOWSCREEN
MOUSEWAIT

FUNCTION PUTATEXT$:
RETURN "HELLO"
ENDFUNCTION

FUNCTION PUTATEXT2:
LOCAL B$="HELLO2"
RETURN B$
ENDFUNCTION

FUNCTION PUTATEXT3: C$
INC C$," MY FRIEND"
RETURN C$
ENDFUNCTION
Title: Re: Unexpected behavior from a function
Post by: Alex_R on 2015-Aug-06
Oh!! Thanks (gracias) mentalthink. You are right!! I forgot to put the $ symbol in the function. A newbie mistake, he, he!!  :whistle: I think its the first time I need to return a string from a function and I didn't realize this detail.  ::)
Title: Re: Unexpected behavior from a function
Post by: MrTAToad on 2015-Aug-06
Yes, the correct return type is always needed, otherwise you can get unexpected problems  =D
Title: Re: Unexpected behavior from a function
Post by: mentalthink on 2015-Aug-06
Thanks to Alex_R for the mistake and mrT for clarify, I never put any type return in any program I did  :-[ , I think my C++ learning is doing effect  =D
Title: Re: Unexpected behavior from a function
Post by: spacefractal on 2015-Aug-06
There is actuelly two commands that does pretty much the same thing:

DEBUG and STDOUT.

on iOS, im was forced to deprecating the DEBUG command, because its conflicted a xcode compiler directive issue, and was impossible to avoid it. So im have with glbasic source code routed all DEBUG calls to STDOUT instead.

Due above, im did a little function, which eventuelly could log it to a file (writing to file does require a little change, but you have the idea):

Code (glbasic) Select

FUNCTION DEPRINT: Value$
STATIC LASTVALUE$
IF PLATFORMINFO$("")="MACOSX" THEN RETURN
IF LASTVALUE$=Value$ THEN RETURN
LASTVALUE$=Value$
// IF SaveLog$<>""
// LOCAL ok=OPENFILE(3, SaveLog$, -1)
// IF ok=1
// WRITELINE 3, Value$
// CLOSEFILE 3
// RETURN
// ENDIF
// ENDIF

?IFDEF IPHONE
STDOUT "[GLB]->" + Value$ + "\n"
?ELSE
IF PLATFORMINFO$("DEVICE")="DESKTOP"
STDOUT "[GLB]->" + Value$ + "\n"
DEBUG Value$
DEBUG "\n"
ELSE
STDOUT "[GLB]->" + Value$ + "\n"
ENDIF
?ENDIF
ENDFUNCTION