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.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Use The Comprehensive Form Properties In C++ VCL Apps

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

This Is How To Add Characters to Wide Strings in Modern C++

C++ComponentsLanguage FeatureLearn C++

How To Create Gorgeous Transparent App Windows With C++

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

This Is How To Get A Substring of a Wide String in C++