The most versatile system I've seen for this kind of thing is Common Lisp's. Any function can have:
- 0 or more required arguments
- 0 or more optional arguments - with (optional) user-specified defaults
- 0 or more optional "keyword" arguments -
- 0 or 1 "rest" argument which gets a list of all the extra arguments the user passes in that don't get bound to another argument.
You would specify it like this:
Code:
(defun func ( a b &optional c (d 12) &key e (f "abc") &rest g)
...even omiting additional syntax which let the function figure out whether the user specified an optional parameter or whether it got its default value.
ASH has:
- 0 or more required arguments on main()
- the last of which acts as a "rest" argument, if the user passes in extra stuff.
I don't think I have time to do the language design to decide on pleasant syntax to improve on that - or to implement it, once such a syntax is defined. I imagine that the original suggestion of including " = default value " is not too bad, but it seems like it will be tricky to implement. You can already overload functions. So, if I define the following:
Code:
int func( int a, int b = 2)
...
int func( int a )
then, which function do we call if you then code:
Do we call the first one, with second parameter bound to the default value of 2, or do we call the second one?
Given that, I think it'd be best to just do something special with main() - either allow special syntax for arguments there, or allow it to be overloaded. I think the latter would be confusing.
Specifying "main", "mainnp", and "mainna" as variant versions of main to call depending on what command line arguments the user typed seems like a kludge to me. Sorry.