Different number of arguments in a function

Previous topic - Next topic

mentalthink

HI I like to know if directly in Glbasic, can pass a different number of arguments in a same function.

I look a C++ tutorial on explain this, and only it´s for know it... this can be do directly from Glbasic?¿, or is INLINE command needed.

Thanks,

Iván J.

backslider

What do you mean with a different number of arguments?

Do you mean real overloading of functions?
Code (glbasic) Select

function a: a%, b%
endfunction

function a: a%
endfunction


If yes, no it's not possible in GLBasic.

If you mean different arguments in one function, it's possible!
Code (glbasic) Select

function a: a%=10, b$="Hello", c#=1.0
endfunction

a(5)
a(5, "blah")
a(5, "bla", 3.2)


cheers

Kitty Hello

variable arguments are not supported. It's a feature that's not really type safe and very dangerous to use. Example:
printf("%d %d", 6) // beeeeeep - no 2nd argument given, no compiler error, runtime crash/strange behaviour.


mentalthink

Ok, thanks for all the replies, Masters.

MrTAToad

You could also set default values for FUNCTION variables and sort of emulate it that way.

mentalthink


Slydog

How hard would function overloading be to implement?
Doesn't C++ support this natively?

So many times I wanted to have the same function name accept different data types!
The obvious work around is to name them differently, but then you have to remember those 'non-obvious' names.

Or, could GLBasic add a suffix to each function name behind the scenes when compiling that states the data types accepted?
FUNCTION Add: a%, b%
would compile to
FUNCTION Add_ii: a%, b%

FUNCTION Add: a#, b#
would compile to
FUNCTION Add_ff: a%, b%

Then of course you would need to identify at compile time what function to use for each call.
I know I'm just dreaming, but thought I'd throw it out there!
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Kitty Hello

then you get stupid compiler warnings for ambiguous calls.
Do other BASICs allow this?

Slydog

Well, VB.Net ! =D

I just find it is a great feature that would work well with TYPE libraries.
I know this isn't really requested by many people, so I can live without it.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

MrTAToad

Good idea - and you automatically know how many parameters to deal with.

hardyx

I'd like more than overloading, the posibility to use parameters in SUBs, like in the funtions. Will be very useful and is supported in many BASIC flavours. For example:

SUB DrawHero: %x, %y, %life, name$
....
ENDSUB


Kitty Hello

why not use a function instead?

backslider

Quote from: Kitty Hello on 2011-Oct-05
then you get stupid compiler warnings for ambiguous calls.
Do other BASICs allow this?

Hm, Monkey does allow overloaded functions, but it's OOP so you can't really compare it with GLBasic.
I would like Function overloading, too!

But then GLBasic goes more and more OOP! :D I think Gernot doesn't want "OOP" in GLBasic.^^

cheers

Slydog

#13
QuoteI think Gernot doesn't want "OOP" in GLBasic.

Ha, ya, that's what I read a while ago too!
But most OOP features would be completely optional to use, you don't NEED to overload your functions, but it could be there for those that want it.  Not sure how difficult it is for Gernot to program that however!

It would be cool also to have an option of GL'Basic' that skips the 'basic' part and lets us develop purely in C++!  8)
Have all the same graphics and input functions still available, but lets us use C++ syntax.  The compiling stage would seem simple! (and yes, I know there's INLINE available but it's not the same!)

What, I'm kicked out of this forum?! ha  :D

My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Kitty Hello

where is the difference of inline and pure C++? I don't get it.

Code (glbasic) Select

INLINE

DGStr str = "Hello World";
PRINT( str, 100, 100);
SHOWSCREEN();
MOUSEWAIT();
ENDINLINE