C++C++11C++14C++17Introduction to C++Learn C++

Learn About Function Declaration and Definition in C++

In C and C++ programming language there are two parts of a function, Declaration, and Definition.

Function Declaration is the combination of the return type of function, function’s name, and parameters in parenthesis.
Function Definition is the body of the function includes statements and other functions.

In general use, they are defined together as below,

For example, an info() function that prints out some information about our application, can be declared and defined as below

and this info function can be used in the main() function as below,

In some cases, we declare and define functions after calling. For example in this same example, If we declare and define the function after the main() function, we will get an error. Because in the line-by-line progress there is no information about the info() function when calling it. In that case, we must declare a function before we call it. To do this we just need the declaration part of the function. The full declaration and definition part should remain after the main function. See the example below,

Note that this info() function has no return value, if our function has a return value and it has parameters you must use as same in the declaration.

Function Declaration and Definition in Headers and C++ Files

We can add many functions in headers and .c or .cpp files. if we have a function declaration and definition in a header,

this is myinfo.h,

this is main.cpp which runs as same as given example above,

as you see we declared and defined function in a .h file and we include this header in the main function. In this example, we can not include this header in different modules (forms or units) because redefinition of the same function is not allowed, compiler conflicts, and results with the error.

In general, headers are used to declare functions, and declarations and definitions of functions are coded in C++ files. This allows users to add the same headers with function declarations. Here is a good example with .h .cpp and the main.cpp file,

this is myinfo.h,

this is myinfo.cpp,

this is main.cpp,

Function Declaration in Classes

C++ is an Object Oriented programming language, objects are defined with classes and most of functions are defined in these classes. Functions can be also defined in in public: or private: sections of classes as below,

this is myinfo.h,

Function declaration outside of the class , we should use the class with :: characters. Here is myinfo.cpp,

External Function Declaration

in some cases, we can use functions between modules, libraries. We can use these declared function by using extern command to declare them externally in our codes, as below,

Get started building powerful apps with C++Builder!


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Related posts
C++Learn C++Videos

Learn C++ With Monoids, Monads, and Applicative Functors: Repeated Software Patterns (CPPCon 2020) Video

C++Learn C++Videos

Learn C++ With Pipes: How Plumbing Can Make Your C++ More Expressive by Jonathan Boccara (CPPCon 2020) Video

C++Introduction to C++Learn C++

Learn Classes and Objects in C++

C++C++11C++14C++17Introduction to C++Learn C++

Learn about Access Specifiers in C++ Classes