Function declaration

A function declaration introduces the function name and its type. A function definition associates the function name/type with the function body.

# Notes

In case of ambiguity between a variable declaration using the direct-initialization syntax and a function declaration, the compiler always chooses function declaration; see direct-initialization.

# Example

#include <iostream>
#include <string>
 
// simple function with a default argument, returning nothing
void f0(const std::string& arg = "world!")
{
    std::cout << "Hello, " << arg << '\n';
}
 
// the declaration is in namespace (file) scope
// (the definition is provided later)
int f1();
 
// function returning a pointer to f0, pre-C++11 style
void (*fp03())(const std::string&)
{
    return f0;
}
 
// function returning a pointer to f0, with C++11 trailing return type
auto fp11() -> void(*)(const std::string&)
{
    return f0;
}
 
int main()
{
    f0();
    fp03()("test!");
    fp11()("again!");
    int f2(std::string) noexcept; // declaration in function scope
    std::cout << "f2(\"bad\"): " << f2("bad") << '\n';
    std::cout << "f2(\"42\"): " << f2("42") << '\n';
}
 
// simple non-member function returning int
int f1()
{
    return 007;
}
 
// function with an exception specification and a function try block
int f2(std::string str) noexcept
try
{
    return std::stoi(str);
}
catch (const std::exception& e)
{
    std::cerr << "stoi() failed!\n";
    return 0;
}
 
// deleted function, an attempt to call it results in a compilation error
void bar() = delete
#   if __cpp_deleted_function
    ("reason")
#   endif
;

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
CWG 135C++98member functions defined in classcould not have a parameter of or returnits own class because it is incompleteallowed
CWG 332C++98a parameter could have cv-qualified void typeprohibited
CWG 393C++98types that include pointers/references toarray of unknown bound could not be parameterssuch types are allowed
CWG 452C++98member initializer list was not a part of function bodyit is
CWG 577C++98dependent type void could be used todeclare a function taking no parametersonly non-dependentvoid is allowed
CWG 1327C++11defaulted or deleted functions could notbe specified with override or finalallowed
CWG 1355C++11only special member functions could be user-providedextended to all functions
CWG 1394C++11deleted functions could not have any parameter ofan incomplete type or return an incomplete typeincomplete type allowed
CWG 1824C++98the completeness check on parameter type andreturn type of a function definition could be madeoutside the context of the function definitiononly check in thecontext of thefunction definition
CWG 1877C++14return type deduction treated return; as return void();simply deduce the returntype as void in this case
CWG 2015C++11the implicit odr-use of a deletedvirtual function was ill-formedsuch odr-uses are exemptfrom the use prohibition
CWG 2044C++14return type deduction on functions returning voidwould fail if the declared return type is decltype(auto)updated the deductionrule to handle this case
CWG 2081C++14function redeclarations could use return typededuction even if the initial declaration does notnot allowed
CWG 2144C++11{} could be a function body or an initializer at the same placedifferentiated by the typeof the declarator identifier
CWG 2145C++98the declarator in function definition could not be parenthesizedallowed
CWG 2259C++11the ambiguity resolution rule regarding parenthesizedtype names did not cover lambda expressionscovered
CWG 2430C++98in the definition of a member function in a class definition,the type of that class could not be the return type orparameter type due to the resolution of CWG issue 1824only check in thefunction body
CWG 2760C++98the function body of a constructor did not include the initializationsnot specified in the constructor’s regular function bodyalso includes theseinitializations
CWG 2831C++20a function definition with a requires-clausecould define a non-templated functionprohibited
CWG 2846C++23explicit object member functions could not have out-of-class definitionsallowed
CWG 2915C++23unnamed explicit object parameters could have type voidprohibited

# See also