33
\$\begingroup\$

Using your language of choice, write a function that takes a variable number of arguments and returns the number of arguments it was called with.

Specifics:

  • Your language needs to support variadic argument functions: something callable that takes an arbitrary number of arguments and returns a value.
  • Parameters must be able to be passed individually. This means that passing an array would only count for one parameter. You can use an "all passed arguments" array if your language supports it; the restriction is on how the function is called.
  • Code that calls this function must not be required to pass the number of arguments in its source. If a compiler inserts the number of arguments as part of a calling convention, that is allowed.
  • The arguments can be of any type you want. You can support only a single type (e.g. only supporting int is still valid), arbitrary types (any type of argument is allowed), or any combination of argument types (e.g. first arg is int, rest are strings).
  • Your function may have a maximum number of arguments (especially since resources are finite), but must support at least 2 arguments.

Samples:

  • f() returns 0
  • f(1) or f("a") returns 1
  • f([1, 2, 3]) returns 1 as it is passed an array, not 3 arguments
  • f(1, 10) or f(1, "a") returns 2

As this is code-golf, the winning solution is the one that uses the fewest number of bytes.

\$\endgroup\$
  • 5
    \$\begingroup\$ It's not entirely clear (objectively) what is a "function", "return value" or "variadic arguments". For example, would Dodos function be considered as monadic or variadic? \$\endgroup\$ – user202729 Apr 10 '18 at 5:06
  • 24
    \$\begingroup\$ @user202729 If your language doesn't support functions, use another language. It's not a requirement that all languages can compete, part of code golfing is finding the right tool for the job. \$\endgroup\$ – Sanchises Apr 10 '18 at 7:46
  • 5
    \$\begingroup\$ @user202729 I have no problems with the occasional challenge aimed at traditional/high-level languages, just as we have the occasional challenge that is only possible in unusual languages. \$\endgroup\$ – Sanchises Apr 10 '18 at 13:58
  • 6
    \$\begingroup\$ didn't know we had to solve the halting problem for language characteristics to have a clear challenge.... \$\endgroup\$ – Conor O'Brien Apr 11 '18 at 2:06
  • 5
    \$\begingroup\$ If your language doesn't have the concept of arguments / calling convention then it doesn't fit the criteria of supporting arbitrary numbers of arguments. \$\endgroup\$ – Glenn Smith Apr 11 '18 at 8:23

69 Answers 69

1 2
3
0
\$\begingroup\$

Funky, 10 bytes

(...a)=>#a

Funky does this pretty well. I first tried @#arguments, however, arguments is never actually used by @ functions, and even if, that turned out larger...

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

C#, 32 bytes

int X(params int[] y)=>y.Length;

Expression-bodied method with the params keyword.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Clojure, 11 bytes

#(count %&)

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

C macro (gcc), 61

  • 3 bytes saved thanks to @ceilingcat.

I think the only way to do this in is with a preprocessor macro:

#define f(a...) ({char*s=#a,i=!!*s;for(;*s;)*s++-44||i++;i;})

Any symbols and literal integers may be passed. But passing literal strings containing commas will certainly mess this up.

This can probably be golfed more, but for now its a proof-of-concept.

The arg counter is declared as a char, and is thusly size-limited.

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You can remove the space between f(a...) and ({char*s \$\endgroup\$ – ceilingcat Jun 16 '18 at 3:39
0
\$\begingroup\$

Visual Basic .NET (.NET Core), 54 49 bytes

Unfortunately, lambda expressions do not allow ParamArray in VB.net.

Function F(ParamArray a())
F=a.Count:End Function

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 49? \$\endgroup\$ – ASCII-only Jun 9 '18 at 5:35
  • \$\begingroup\$ @ASCII-only Oh, I forgot that's how values used to be returned in old VB syntax. That should work. \$\endgroup\$ – mbomb007 Jun 9 '18 at 19:28
0
\$\begingroup\$

Python 2,22 21 15 bytes

lambda*a:len(a)

EDIT: -1 byte by removing space between "lambda" and "*a"

Edit 2: -6 thanks to @ppperry

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ I believe you can save a few bytes by removing the "print" \$\endgroup\$ – pppery Aug 7 '18 at 3:00
0
\$\begingroup\$

05AB1E, 2 bytes

|g

Try it online.

Explanation:

|     # Take all new-line separated inputs as a single list
 g    # Take the length of this list
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

W, 1 byte

If I'm understanding it correctly:

k

Why does this work?

Assume that the program is a function (the program is technically a function). In this case, you pass arguments into the function in a list like this:

[[item1,item2,item3]]

Because the main list only supports at most 4 items, we put the arguments in a sub-list of that list (just an optimization, no effect for the source code).

Also, the program technically reads that list of arguments:

ak

Then it finds the length of a list of its arguments.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Pyth, 9 bytes

D[$*N$)lN

Try it online!

Variadic functions cannot be defined in pure Pyth, but using the raw Python override $$, it's easy. This translates to the Python program:

def list(*N):
    print(len(N))

Where [ is the function defined.

| improve this answer | |
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.