Author Topic: Optional Arguments Within a Function  (Read 1470 times)

Offline CW

  • Mr. Polyvector
  • ***
  • Posts: 155
    • View Profile
Optional Arguments Within a Function
« on: 2013-Mar-20 »
Hey all, I need someone to help jog my memory.

I seem to remember reading somewhere that it is possible to declare optional arguments which can be passed to a function, but which can be omitted; in which case the function uses the default value. I can't find that text again, now that I need the ability. (Was it in GLbasic?)

It would look something like: (Just a quick and dirty example..)

FUNCTION Example: x%,y%, [color% = 0]    //where color% can either be passed, or not, by the calling program.
  SETPIXEL x,y,color
ENDFUNCTION

If memory serves, optional arguments are allowed provided they are placed at the end of the declaration.
I COULD create two or more variations on the same function, or create nesting wrappers around the functions, but why create two when one will do?

Can someone give me a hand?   :)

-CW
« Last Edit: 2013-Mar-20 by CW »

MrTAToad

  • Guest
Re: Optional Arguments Within a Function
« Reply #1 on: 2013-Mar-20 »
You do something like

Code: (glbasic) [Select]
FUNCTION a%:t%=1,t2%=5
ENDFUNCTION

You can then do :


Code: (glbasic) [Select]
g=a()
g=a(1)
g=a(2,3)

Offline CW

  • Mr. Polyvector
  • ***
  • Posts: 155
    • View Profile
Re: Optional Arguments Within a Function
« Reply #2 on: 2013-Mar-20 »
Thanks Mr.Toad, that's just what I was looking for.
I was adding a lot of extra garbage which I didn't need.

Cheers!   :happy:
-CW