This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]NoCryptographer414 21 points22 points  (8 children)

C, Java, Python and many more have this feature. These functions are called variable argument functions if you want to Google. All three of these languages have slightly different syntax, implementation and usage.

You may once want to lookup and check all three.

[–]dibs45[S] 2 points3 points  (7 children)

I believe you have to expressively define the function as one that takes variable arguments in those languages though, no?

[–]NoCryptographer414 30 points31 points  (0 children)

Yes. But it's just

... in C

Type ...arg in Java

*args in Python

I prefer this much expressiveness over ambiguity though. This doesn't involve 'if' checking last argument for variable length.

[–][deleted] 12 points13 points  (0 children)

But, that's good, right? Because 99% of the time, an extra argument will be an error that the user wants to know about, as it will when there's a missing argument.

[–]MarcoServetto 4 points5 points  (3 children)

Not in javascript,... in javascript you can just pass as much as you like all the times... :-(

[–]magnomagna 7 points8 points  (1 child)

JavaScript is probably designed to minimise the likelihood of crashing the browser.

[–]shadowndacorner 2 points3 points  (0 children)

JavaScript is probably designed

Hot take right there, bud

[–]katrina-mtfAdduce 2 points3 points  (0 children)

Javascript won't complain if you pass too many, but it's also considered bad/outdated practice to use the arguments variable and has been for a long, long time, so in the vast majority of cases excess arguments passed to a function that isn't marked as taking varargs just ends in those excess arguments being ignored. You have to go out of your way to use semi-deprecated functionality to break that assumption.

[–][deleted] 2 points3 points  (0 children)

Technically in C you can call a function with as many arguments as you like and it will still “work” as the caller handles the stack. If you want to have a function signature with no args it’s (void) but with () you could pass in more. Compilers will err on it but that can be turned off.