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,
1 2 3 4 5 6 |
myfunction() // this is the function declaration { // the definition of the function (statements and other functions) } |
For example, an info() function that prints out some information about our application, can be declared and defined as below
1 2 3 4 5 6 7 8 9 |
info() { std::cout << "This is my application.\n"; std::cout << "Here is my information.\n"; std::cout << "Developed in 2021 by My Company\n"; std::cout << '\n'; } |
and this info function can be used in the main() function as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> info() { std::cout << "This is my application.\n"; std::cout << "Here is my information.\n"; std::cout << "Developed in 2021 by My Company\n"; std::cout << '\n'; } int main() { info(); // calling info function } |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> info(); // decleration int main() { info(); // calling info function } info() { std::cout << "This is my application.\n"; std::cout << "Here is my information.\n"; std::cout << "Developed in 2021 by My Company\n"; std::cout << '\n'; } |
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,
1 2 3 4 5 6 7 8 9 |
info() { std::cout << "This is my application.\n"; std::cout << "Here is my information.\n"; std::cout << "Developed in 2021 by My Company\n"; std::cout << '\n'; } |
this is main.cpp which runs as same as given example above,
1 2 3 4 5 6 7 8 9 |
#include <iostream> #include <myinfo.h> int main() { info(); // calling info function } |
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,
1 2 3 |
info(); |
this is myinfo.cpp,
1 2 3 4 5 6 7 8 9 |
info() { std::cout << "This is my application.\n"; std::cout << "Here is my information.\n"; std::cout << "Developed in 2021 by My Company\n"; std::cout << '\n'; } |
this is main.cpp,
1 2 3 4 5 6 7 8 9 |
#include <iostream> #include <myinfo.h> int main() { info(); // calling info function } |
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,
1 2 3 4 5 6 7 |
class myclass { public: info(); } |
Function declaration outside of the class , we should use the class with :: characters. Here is myinfo.cpp,
1 2 3 4 5 6 7 8 9 |
myclass::info() { std::cout << "This is my application.\n"; std::cout << "Here is my information.\n"; std::cout << "Developed in 2021 by My Company\n"; std::cout << '\n'; } |
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,
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <myinfo.h> extern info(); int main() { info(); // calling info function } |