Maximum Number of Parameters in Function Declaration

Maximum number of parameters in function declaration

Yes, there are limits imposed by the implementation. Your answer is given in the bold text in the following excerpt from the C++ Standard.

1. C++ Language


Annex B - Implementation quantities

  1. Because computers are finite, C + + implementations are inevitably limited
    in the size of the programs they can
    successfully process. Every
    implementation shall document those
    limitations where known.
    This
    documentation may cite fixed limits
    where they exist, say how to compute
    variable limits as a function of
    available resources, or say that fixed
    limits do not exist or are unknown.

  2. The limits may constrain quantities that include those described below or
    others. The bracketed number following
    each quantity is recommended as the
    minimum for that quantity.
    However,
    these quantities are only guidelines
    and do not determine compliance.

    Nesting levels of compound statements,
    iteration control structures, and
    selection control structures [256].

    Nesting levels of conditional
    inclusion [256].
    — Pointer, array, and
    function declarators (in any
    combination) modifying an arithmetic,
    structure, union, or incomplete type
    in a declaration [256].
    — Nesting
    levels of parenthesized expressions
    within a full expression [256].

    Number of characters in an internal
    identifier or macro name [1 024].

    Number of characters in an external
    identifier [1 024].
    — External
    identifiers in one translation unit
    [65 536].
    — Identifiers with block
    scope declared in one block [1 024].

    Macro identifiers simultaneously
    defined in one translation unit [65
    536].
    — Parameters in one function
    definition [256].
    — Arguments in one
    function call [256].

    — Parameters in
    one macro definition [256].

    Arguments in one macro invocation
    [256].
    — Characters in one logical
    source line [65 536].
    — Characters in
    a character string literal or wide
    string literal (after concatenation)
    [65 536].
    — Size of an object [262
    144].
    — Nesting levels for #include
    files [256].
    — Case labels for a
    switch statement (excluding those for
    any nested switch statements) [16
    384].
    — Data members in a single
    class, structure, or union [16 384].

    Enumeration constants in a single
    enumeration [4 096].
    — Levels of
    nested class, structure, or union
    definitions in a single
    struct-declaration-list [256].

    Functions registered by atexit()[32].

    — Direct and indirect base classes [16
    384].
    — Direct base classes for a
    single class [1024].
    — Members
    declared in a single class [4 096].

    Final overriding virtual functions in
    a class, accessible or not [16 384].

    Direct and indirect virtual bases of a
    class [1 024].
    — Static members of a
    class [1 024].
    — Friend declarations
    in a class [4 096].
    — Access control
    declarations in a class [4 096].

    Member initializers in a constructor
    definition [6 144].
    — Scope
    qualifications of one identifier
    [256].
    — Nested external
    specifications [1 024].
    — Template
    arguments in a template declaration [1
    024].
    — Recursively nested template
    instantiations [17].
    — Handlers per
    try block [256].
    — Throw
    specifications on a single function
    declaration [256].

Besides, it also says in $18.3/6,

Implementation Limits: The
implementation shall support the
registration of at least 32 functions.

Maximum number of parameters in Java method declaration

That limit is defined in the JVM Specification:

The number of method parameters is limited to 255 by the definition of a method descriptor (§4.3.3), where the limit includes one unit for this in the case of instance or interface method invocations.

Section §4.3.3 gives some additional information:

A method descriptor is valid only if it represents method parameters with a total length of 255 or less, where that length includes the contribution for this in the case of instance or interface method invocations.

The total length is calculated by summing the contributions of the individual parameters, where a parameter of type long or double contributes two units to the length and a parameter of any other type contributes one unit.

Your observations were spot on, double word primitives (long/double) need twice the size of usual 4 bytes variables and 4 bytes object instance references.

Regarding the last part of your question related to 64bit systems, the specification defines how many units a parameter contribute, that part of the specification must still be complied with even on a 64bit platform, the 64bit JVM will accomodate 255 instance parameters (like your 255 Strings) regardless of the internal object's pointer size.

Is there a max number of arguments JavaScript functions can accept?

Although there is nothing specific limiting the theoretical maximum number of arguments in the spec (as thefortheye's answer points out). There are of course practical limits. These limits are entirely implementation dependent and most likely, will also depend exactly on how you're calling the function.


I created this fiddle as an experiment.

function testArgs() {
console.log(arguments.length);
}

var argLen = 0;
for (var i = 1; i < 32; i++) {
argLen = (argLen << 1) + 1;
testArgs.apply(null, new Array(argLen));
}

Here are my results:

  • Chrome 33.0.1750.154 m: The last successful test was 65535 arguments. After that it failed with:

    Uncaught RangeError: Maximum call stack size exceeded

  • Firefox 27.0.1: The last successful test was 262143 arguments. After that it failed with:

    RangeError: arguments array passed to Function.prototype.apply is too large

  • Internet Explorer 11: The last successful test was 131071 arguments. After that it failed with:

    RangeError: SCRIPT28: Out of stack space

  • Opera 12.17: The last successful test was 1048576 arguments. After that it failed with:

    Error: Function.prototype.apply: argArray is too large

Of course, there may be other factors at play here and you may have different results.


And here is an alternate fiddle created using eval. Again, you may get different results.

  • Chrome 33.0.1750.154 m: The last successful test was 32767 arguments. After that it failed with:

    Uncaught SyntaxError: Too many arguments in function call (only 32766 allowed)

    This one is particularly interesting because Chrome itself seems to be confused about how many arguments are actually allowed.

  • Firefox 27.0.1: The last successful test was 32767 arguments. After that it failed with:

    script too large

  • Internet Explorer 11: The last successful test was 32767 arguments. After that it failed with:

    RangeError: SCRIPT7: Out of memory

  • Opera 12.17: The last successful test was 4194303 arguments. After that it failed with:

    Out of memory; script terminated.

How many parameters are too many?

When is something considered so obscene as to be something that can be regulated despite the 1st Amendment guarantee to free speech? According to Justice Potter Stewart, "I know it when I see it." The same holds here.

I hate making hard and fast rules like this because the answer changes not only depending on the size and scope of your project, but I think it changes even down to the module level. Depending on what your method is doing, or what the class is supposed to represent, it's quite possible that 2 arguments is too many and is a symptom of too much coupling.

I would suggest that by asking the question in the first place, and qualifying your question as much as you did, that you really know all of this. The best solution here is not to rely on a hard and fast number, but instead look towards design reviews and code reviews among your peers to identify areas where you have low cohesion and tight coupling.

Never be afraid to show your colleagues your work. If you are afraid to, that's probably the bigger sign that something is wrong with your code, and that you already know it.

What is the maximum number of parameters that a C# method can be defined as taking?

Here is your theoretical answer:

In order to push method arguments onto the stack, compiled code has the following MSIL opcodes to choose from:

ldarg.0

ldarg.1

ldarg.2

ldarg.3

ldarg.S

ldarg

ldarg.0 to ldarg.3 is used to push the first 4 method arguments onto the stack (including this as the first argument for instance methods).

ldarg.S takes an 8-bit argument number, and so it can be used to push up to 256 arguments onto the stack.

That leaves us with plain old ldarg, which can handle the most method arguments: it takes an unsigned 16-bit argument number, so the largest number of arguments that can be successfully compiled into valid MSIL is 2^16 = 65,536.

As others have noted, however, the actual limit will depend on implementation details of the runtime. Based on rmiesen's answer, it looks like the current .NET implementation limits the maximum number of parameters to 2^14.

How many parameters are too many in JavaScript?

Argument length limited to 65536
https://bugs.webkit.org/show_bug.cgi?id=80797

There are different argument count limits, depending on how you test: http://mathiasbynens.be/demo/javascript-argument-count

PHP Functions - Maximum number of arguments

Arguments to a function are pushed on a stack, after which the function is called which in turn reads the stack and uses those values as parameters.

So as long as the stack isn't full, you can keep adding parameters, but it'll depend on the situation, and at design-time you won't know the stack size.

But I really hope this is pure a technical discussion and you don't need it IRL. ;-)

What is a maximum number of arguments in a Python function?

In Python 3.7 and newer, there is no limit. This is the result of work done in issue #27213 and issue #12844; #27213 reworked the CALL_FUNCTION* family of opcodes for performance and simplicity (part of 3.6), freeing up the opcode argument to only encode a single argument count, and #12844 removed the compile-time check that prevented code with more arguments from being compiled.

So as of 3.7, with the EXTENDED_ARG() opcode, there is now no limit at all on how many arguments you can pass in using explicit arguments, save how many can be fitted onto the stack (so bound now by your memory):

>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=7, micro=0, releaselevel='alpha', serial=2)
>>> def f(*args, **kwargs): pass
...
>>> exec("f({})".format(', '.join(map(str, range(256)))))
>>> exec("f({})".format(', '.join(map(str, range(2 ** 16)))))

Do note that lists, tuples and dictionaries are limited to sys.maxsize elements, so if the called function uses *args and/or **kwargs catch-all parameters then those are limited.

For the *args and **kwargs call syntax (expanding arguments) there are no limits other than the same sys.maxint size limits on Python standard types.

In versions before Python 3.7, CPython has a limit of 255 explicitly passed arguments in a call:

>>> def f(*args, **kwargs): pass
...
>>> exec("f({})".format(', '.join(map(str, range(256)))))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
SyntaxError: more than 255 arguments

This limitation is in place because until Python 3.5, the CALL_FUNCTION opcode overloaded the opcode argument to encode both the number of positional and keyword arguments on the stack, each encoded in a single byte.



Related Topics



Leave a reply



Submit