Author Topic: Functions as Parameters  (Read 2553 times)

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2788
    • View Profile
Functions as Parameters
« 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.
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline backspace

  • Mr. Drawsprite
  • **
  • Posts: 53
    • View Profile
Re: Functions as Parameters
« Reply #1 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.
« Last Edit: 2013-Jan-24 by backspace »
I came, I saw, I coded.

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2788
    • View Profile
Re: Functions as Parameters
« Reply #2 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.
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline backspace

  • Mr. Drawsprite
  • **
  • Posts: 53
    • View Profile
Re: Functions as Parameters
« Reply #3 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()
I came, I saw, I coded.

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2788
    • View Profile
Re: Functions as Parameters
« Reply #4 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.
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)