Declaring functions

From cppreference.com
 
 
C++ language
General topics
Preprocessor
Comments
Keywords
ASCII chart
Escape sequences
History of C++
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
union types
function types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

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

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

void function() {
   std::cout << "Hello, World!" << std::endl;
   //no return
}

Output:

Hello, World!