Declaring functions
From cppreference.com
A function declaration introduces the function name and its type. It may appear in any scope, and is commonly placed in header files.
ret name ( params ) cv ref except attr | (1) | ||||||||
auto name ( params ) cv ref except attr -> ret | (2) | (since C++11) | |||||||
A function definition provides the body of a function. It may only appear in namespace or class scope.
attr decl name ( params ) cv ref except attr -> ret virt try init-list { body } catch | (3) | ||||||||
attr decl name ( params ) cv ref except attr -> ret virt = 0 ; | (4) | ||||||||
attr decl name ( params ) cv ref except attr -> ret virt = default ; | (5) | (since C++11) | |||||||
attr decl name ( params ) cv ref except attr -> ret virt = delete ; | (6) | (since C++11) | |||||||
[edit] Explanation
This section is incomplete |
attr(C++11) | - | Optional sequence of any number of function attributes, such as [[noreturn]] or [[carries_dependency]]. May appear both before and after the function name | ||
ret | - | the type returned by the function, may be void if the function returns nothing. Cannot be array or function type, although can be a pointer or reference to such. Required for all functions except constructors, destructors, and conversion operators, which must not provide a return type. | ||
decl | - | declaration specifier sequence, which consists of none or some of the following keywords: static, extern, inline, virtual, explicit, friend, constexpr, combined with the return type, ret | ||
cv | - | Optional const, volatile, or const volatile, only applicable to non-static member functions. For a member function of class T, the type of this will be const T*, volatile T*, or const volatile T* respectively. A member function declared const cannot modify members of *this. | ||
ref(C++11) | - | Optional & or &&, only applicable to non-static member functions. For a member function of class T, the type of this will be T& or T&& respectively. | ||
except | - | either dynamic-exception-specification or noexcept-specification | ||
virt(C++11) | - | Optional override or final, only applicable to non-static member functions | ||
->ret(C++11) | - | Trailing return type, only applicable if ret is auto. Useful if the type depends on argument names, such as template <class T, class U> auto add(T t, U u) -> decltype(t + u); or is complicated, such as in auto fpif(int)->int(*)(int) | ||
init-list | - | Constructor initializer list, only used in constructors | ||
try | - | Optional function try block. If present, catch must be provided | ||
catch | - | Optional sequence of catch-blocks, only applicable of try is used. | ||
body | - | The body of the function, a (possibly empty) compound statement | ||
params | - | The list of parameters
|
[edit] Example 1
Output:
Hello, World!