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.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?