Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Function definitions

      From cppreference.com
      <c‎ |language
       
       
       
       

      A function definition associates the function body (a sequence of declarations and statements) with the function name and parameter list. Unlikefunction declaration, function definitions are allowed at file scope only (there are no nested functions).

      C supports two different forms of function definitions:

      attr-spec-seq(optional)specifiers-and-qualifiersparameter-list-declaratorfunction-body (1)
      specifiers-and-qualifiersidentifier-list-declaratordeclaration-listfunction-body (2)(until C23)

      where

      attr-spec-seq -(C23)an optional list ofattributes, applied to the function
      specifiers-and-qualifiers - a combination of
      parameter-list-declarator - a declarator for a function type which uses aparameter list to designate function parameters
      identifier-list-declarator - a declarator for a function type which uses aidentifier list to designate function parameters
      declaration-list - sequence of declarations that declare every identifier inidentifier-list-declarator. These declarations cannot use initializers and the onlystorage-class specifier allowed isregister.
      function-body - acompound statement, that is a brace-enclosed sequence of declarations and statements, that is executed whenever this function is called
      1) New-style (C89) function definition. This definition both introduces the function itself and serves as a function prototype for any futurefunction call expressions, forcing conversions from argument expressions to the declared parameter types.
      int max(int a,int b){return a>b?a:b;} double g(void){return0.1;}
      2)(until C23) Old-style (K&R) function definition. This definition does not behave as a prototype and any futurefunction call expressions will perform default argument promotions.
      int max(a, b)int a, b;{return a>b?a:b;}double g(){return0.1;}

      Contents

      [edit]Explanation

      As withfunction declarations, the return type of the function, determined by the type specifier inspecifiers-and-qualifiers and possibly modified by thedeclarator as usual indeclarations, must be a complete non-array object type or the typevoid. If the return type would be cvr-qualified, it is adjusted to its unqualified version for the purpose of constructing the function type.

      void f(char*s){puts(s);}// return type is voidint sum(int a,int b){return a+b;}// return type is intint(*foo(constvoid*p))[3]{// return type is pointer to array of 3 intreturnmalloc(sizeof(int[3]));}

      As withfunction declarations, the types of the parameters are adjusted from functions to pointers and from arrays to pointers for the purpose of constructing the function type and the top-level cvr-qualifiers of all parameter types are ignored for the purpose of determiningcompatible function type.

      Unlikefunction declarations, unnamed formal parameters are not allowed (otherwise, there would be conflicts in old-style (K&R) function definitions), they must be named even if they are not used within the function. The only exception is the special parameter list(void).

      (until C23)

      Formal parameters may be unnamed in function definitions, because old-style (K&R) function definitions has been removed. Unnamed parameters are inaccessible by name within the function body.

      (since C23)
      int f(int,int);// declaration// int f(int, int) { return 7; } // Error until C23, OK since C23int f(int a,int b){return7;}// definitionint g(void){return8;}// OK: void doesn't declare a parameter

      Within the function body, every named parameter is anlvalue expression, they have automaticstorage duration andblock scope. The layout of the parameters in memory (or if they are stored in memory at all) is unspecified: it is a part of thecalling convention.

      int main(int ac,char**av){    ac=2;// parameters are lvalues    av=(char*[]){"abc","def",NULL};    f(ac, av);}

      Seefunction call operator for other details on the mechanics of a function call andreturn for returning from functions.

      __func__

      Within everyfunction-body, the special predefined variable__func__ with block scope and static storage duration is available, as if defined immediately after the opening brace by

      staticconstchar __func__[]="function name";

      This special identifier is sometimes used in combination with thepredefined macro constants__FILE__ and__LINE__, for example, byassert.

      (since C99)

      [edit]Notes

      The argument list must be explicitly present in the declarator, it cannot be inherited from a typedef

      typedefint p(int q,int r);// p is a function type int(int, int)p f{return q+ r;}// Error

      In C89,specifiers-and-qualifiers was optional, and if omitted, the return type of the function defaulted toint (possibly amended by thedeclarator).

      In addition, old-style definition didn't require a declaration for every parameter indeclaration-list. Any parameter whose declaration was missing had typeint

      max(a, b)// a and b have type int, return type is int{return a>b?a:b;}
      (until C99)

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C standards.

      DRApplied toBehavior as publishedCorrect behavior
      DR 423C89the return type might be qualifiedthe return type is implicitly disqualified

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 6.9.1 Function definitions (p: 113-115)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.9.1 Function definitions (p: 156-158)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.9.1 Function definitions (p: 141-143)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.7.1 Function definitions

      [edit]See also

      C++ documentation forFunction definition
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/function_definition&oldid=174898"

      [8]ページ先頭

      ©2009-2025 Movatter.jp