Maximum number of parameters in function declaration
Asked Answered
P

4

41

I know that minimum number of parameters in function definition is zero, but what is the maximum number of parameters in function definition? I am asking the question just for the sake of knowledge and out of curiosity, not that I am going to write a real function.

Powers answered 3/1, 2011 at 4:11 Comment(1)
Such limits are always defined in documentation of your compiler. IOW you need to name particular compiler to get the exact numbers. These are large for common usage. Several coding policies suggest not to make functions with more than 5 arguments (way less than usually allowed).Theomorphic
P
59

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.

Powers answered 3/1, 2011 at 4:14 Comment(3)
Got a link to the Standard online?Bummalo
@Bummalo ANSI and ISO make money by selling copies of the Standard. It is not freely available; if you need the Standard, you (or your employer) must pay. However, there are pre-finalization drafts available if that is good enough for your purposes. I don't know any C++ drafts, but C draft standards can be found by googling n1256.Lightweight
What does "Size of an object" mean exactly? Does that include the encapsulated data, or is it just for the sum of the sizes of the types of the members (lots of OFs there)?Vu
P
34

2. C Language


5.2.4.1 Translation Limits


The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:
— 127 nesting levels of blocks
— 63 nesting levels of conditional inclusion
— 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or incomplete type in a declaration
— 63 nesting levels of parenthesized declarators within a full declarator
— 63 nesting levels of parenthesized expressions within a full expression
— 63 significant initial characters in an internal identifier or a macro name (each universal character name or extended source character is considered a single character)
— 31 significant initial characters in an external identifier (each universal character name
specifying a short identifier of 0000FFFF or less is considered 6 characters, each universal character name specifying a short identifier of 00010000 or more is considered 10 characters, and each extended source character is considered the same number of characters as the corresponding universal character name, if any)
— 4095 external identifiers in one translation unit
— 511 identifiers with block scope declared in one block
— 4095 macro identifiers simultaneously defined in one preprocessing translation unit
— 127 parameters in one function definition
— 127 arguments in one function call

— 127 parameters in one macro definition
— 127 arguments in one macro invocation
— 4095 characters in a logical source line
— 4095 characters in a character string literal or wide string literal (after concatenation)
— 65535 bytes in an object (in a hosted environment only)
— 15 nesting levels for #included files
— 1023 case labels for a switch statement (excluding those for any nested switch statements)
— 1023 members in a single structure or union
— 1023 enumeration constants in a single enumeration
— 63 lev els of nested structure or union definitions in a single struct-declaration-list

However, it says in notes that,

13) Implementations should avoid imposing fixed translation limits whenever possible.

Powers answered 3/1, 2011 at 4:28 Comment(2)
do u have any link for this documentation ?Claytonclaytonia
@Mr.32: Google for C language standard n1256. On searching, I got this link : open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdfPowers
A
10

As previous answerers have adequately noted, the number of parameters depends on the C++ compiler. The compiler limit may well be due to limitations imposed by hardware environments.

For example the VAX CPU architecture uses an 8-bit field which holds the number of arguments, so compliant VAX code cannot pass more than 255 parameters.

Likewise many small processors like the Intel 8048 family are so constrained for memory that most C and C++ compilers give grief if more about four parameters are used. While many mainstream developers may sneer at the 8048, its descendants remain one of the most widely deployed CPUs—mostly in embedded devices.

Many RISC architecture implementations specify a parameter passing method using registers, rather than the stack. A basic ARM CPU, for example, has only 16 registers. The maximum number of parameters could well depend on compiler command line options which affect how it generates code to pass parameters. Maybe more than eight are pushed onto a stack. Maybe not.

If those examples seem overly restrictive, consider that the Palm OS limited a C function to a single parameter, and that had to be of type int or void *.

Anse answered 3/1, 2011 at 5:31 Comment(0)
T
4

Of course, if you're writing a function in C with such a large number of arguments, you probably want to rethink the design of your program.

Tabby answered 3/1, 2011 at 4:19 Comment(7)
If you're writing a C++ function with more than one argument, you should probably be screaming at the Committee to make tuples easier to use :)Bummalo
This is the best answer. The maximum is "more than you should ever use".Lightweight
I asked this question out of curiosity.Powers
@dsdevsin fair enough. Just a thought.Tabby
Some legitimate cases that may approach these limits are frameworks, source obfuscators and maybe some others. The user should never write by hand anything approaching the limits, though, and should never have to deal with/see such monsters.Angleworm
I am developing an SNMP agent, and an automatic mib2code function generates a create function with one parameter for each mib leaf (and I can have +100 leaves)Yawl
This should be a comment, as it doesn't actually answer the question.Nita

© 2022 - 2024 — McMap. All rights reserved.