GLBasic forum

Main forum => Bug Reports => Topic started by: bigsofty on 2013-Jan-24

Title: Functions as Parameters
Post by: bigsofty on 2013-Jan-24
DIMPUSH w$[], MyFunc(t$[1])

"0" is always pushed onto w$[]. It makes nor difference if MyFunc() has a ByRef or pass by value, the return value is ignored by DIMPUSH.
Title: Re: Functions as Parameters
Post by: backspace on 2013-Jan-24
It works ok if the function is named MyFunc$ so that it can return a string. Set Debug mode, and check the output.

Here's my test
Code (glbasic) Select

GLOBAL w$[]
GLOBAL t$[]
LOCAL m$

DIMPUSH t$[], "123"
DIMPUSH t$[], "456"
DIMPUSH t$[], "789"

m$ = MyFunc$("abc")
DEBUG m$ + "\n"   //<-- outputs "helloabc"

DIMPUSH w$[], MyFunc$(t$[1])
DEBUG w$[0] + "\n"    //<-- outputs "hello456"

FUNCTION MyFunc$: tt$
LOCAL x$ = "hello"
x$ = x$ + tt$
RETURN x$
ENDFUNCTION


Edit: I always make my empty arrays global so I can use then inside functions. Not in this case, but it's a habit.
Title: Re: Functions as Parameters
Post by: bigsofty on 2013-Jan-24
Thanks for testing this.

Yes your code works fine.

Unfortunately mine doesn't and I'm not sure why. The function mentioned is part of an extended type, I'll need to experiment more and find out why it's still doing this and if it has something to do with being within and extended type.

Thanks for looking though.
Title: Re: Functions as Parameters
Post by: backspace on 2013-Jan-24
The function works ok inside a TYPE. Unless I am misunderstanding your problem

Code (glbasic) Select
TYPE TESTType
t$[]
w$[]

FUNCTION Test1:
DIM self.t$[0]
DIM self.w$[0]
DIMPUSH self.t$[], "123"
DIMPUSH self.t$[], "456"
DIMPUSH self.t$[], "789"
DIMPUSH self.w$[], self.MyFunc$(self.t$[1])
DEBUG self.w$[0] + "\n"
ENDFUNCTION

FUNCTION MyFunc$: tt$
LOCAL x$ = "hello"
x$ = x$ + tt$
RETURN x$
ENDFUNCTION

ENDTYPE

LOCAL test AS TESTType

test.Test1()
Title: Re: Functions as Parameters
Post by: bigsofty on 2013-Jan-25
Thanks again.  ;)

I am looking at my code listing and I think that I have omitted a single "self." but the compiler has not reported this and went ahead with the function call. It should not have allowed this though as I have "Explicit Declarations" in the project settings. I can't do a compile till tomorrow, I'll look into this a little further then.