Site icon 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,

[crayon-6742cef02ea16053045472/]

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

[crayon-6742cef02ea23567528188/]

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

[crayon-6742cef02ea2a191771588/]

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,

[crayon-6742cef02ea2c782585382/]

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,

[crayon-6742cef02ea2e653874043/]

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

[crayon-6742cef02ea32091454780/]

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,

[crayon-6742cef02ea34292297947/]

this is myinfo.cpp,

[crayon-6742cef02ea35579761209/]

this is main.cpp,

[crayon-6742cef02ea3b089357834/]

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,

[crayon-6742cef02ea3c138360075/]

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

[crayon-6742cef02ea3e851703797/]

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,

[crayon-6742cef02ea3f875127066/]

Get started building powerful apps with C++Builder!

Exit mobile version