# How many arguments were passed?

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.

• 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? – user202729 Apr 10 '18 at 5:06
• @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. – Sanchises Apr 10 '18 at 7:46
• @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. – Sanchises Apr 10 '18 at 13:58
• didn't know we had to solve the halting problem for language characteristics to have a clear challenge.... – Conor O'Brien Apr 11 '18 at 2:06
• 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. – Glenn Smith Apr 11 '18 at 8:23

# 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!

# C#, 32 bytes

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


Expression-bodied method with the params keyword.

# Clojure, 11 bytes

#(count %&)


Try it online!

# 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!

• You can remove the space between f(a...) and ({char*s – ceilingcat Jun 16 '18 at 3:39

# 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!

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

# 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

• I believe you can save a few bytes by removing the "print" – pppery Aug 7 '18 at 3:00

# 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


## 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.

# 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.