September 19, 2024

What is the difference between function declaration and definition in C or C++?

In this brief tutorial, we explain the difference between function declaration and definition in C and C++. This topic and this question can often be confusing for beginners as well as for advanced C or C++ programmers. The YouTube video accompanying this post is given below.

Copyright Notices and NOT to Be Used for AI Notice

COPYRIGHT NOTICE: THE TEXT, PHOTOS, AND CODES POSTED ON THIS WEBSITE AND ON THIS WEBPAGE ARE THE OWNERSHIP, INTELLECTUAL PROPERTY, AND COPYRIGHTED BY THE AUTHOR: ALEKSANDAR HABER. THE TEXT AND THE CONTENT OF THIS PAGE SHOULD NOT BE PHYSICALLY COPIED, SHOULD NOT BE COPIED ON OTHER WEBSITES, SHOULD NOT BE REPRINTED, SHOULD NOT BE REPOSTED ON OTHER WEBSITES, SHOULD NOT BE USED AS A LECTURE MATERIAL IN UNIVERSITY COURSES, SHOULD NOT BE USED AS LECTURE MATERIAL IN COURSES ORGANIZED BY AND HOSTED ON ONLINE LEARNING PLATFORMS (such as Udemy, Coursera, etc), AND SHOULD NOT BE USED IN COMMERCIAL SETTING. THE TEXT, PHOTOS, AND CODE PRESENTED ON THIS WEBSITE AND WEBPAGE SHOULD NOT BE INCLUDED IN REPORTS, STUDENT PAPERS, SCIENTIFIC PAPERS, OR IN ANY OTHER PRINTED OR A DIGITAL FORM OR A DOCUMENT WITHOUT WRITTEN CONSENT OF THE AUTHOR. A MONEY FEE MIGHT BE REQUIRED TO REPRINT THE MATERIAL POSTED ON THIS WEBSITE: CONTACT THE AUTHOR: ml.mecheng@gmail.com

CODE COPYRIGHT NOTICE AND LICENSE: THE CODE FILES POSTED ON THIS WEBSITE ARE NOT FREE SOFTWARE AND CODE. IF YOU WANT TO USE THIS CODE IN THE COMMERCIAL SETTING OR ACADEMIC SETTING, THAT IS, IF YOU WORK FOR A COMPANY OR IF YOU ARE AN INDEPENDENT CONSULTANT AND IF YOU WANT TO USE THIS CODE OR IF YOU ARE ACADEMIC RESEARCHER OR STUDENT, THEN WITHOUT MY PERMISSION AND WITHOUT PAYING THE PROPER FEE, YOU ARE NOT ALLOWED TO USE THIS CODE. YOU CAN CONTACT ME AT
ml.mecheng@gmail.com
TO INFORM YOURSELF ABOUT THE LICENSE OPTIONS AND FEES FOR USING THIS CODE. ALSO, IT IS NOT ALLOWED TO (1) MODIFY THIS CODE IN ANY WAY WITHOUT MY PERMISSION. (2) INTEGRATE THIS CODE IN OTHER PROJECTS WITHOUT MY PERMISSION. (3) POST THIS CODE ON ANY PRIVATE OR PUBLIC WEBSITES OR CODE REPOSITORIES. DELIBERATE OR INDELIBERATE VIOLATIONS OF THIS LICENSE WILL INDUCE LEGAL ACTIONS AND LAWSUITS.

NOT TO BE USED FOR AI COPYRIGHT NOTICE: The code and text, as well as all other material on this webpage and the YouTube page, should NOT be used to train an AI algorithm or a large language model, or any type of AI or machine learning algorithm used to recognize, interpret, and draw conclusions from text. Also, it is forbidden to crawl this webpage and to extract information for training an AI algorithm of any sort on the basis of the material presented on this webpage.

Declaration of Function in C and C++

Let us start with a function declaration. The function declaration is used to provide the compiler with the following information about the function:

  • Name of the function. The declaration of the function specifies the name of the function that the compiler needs to know.
  • The number of input arguments (parameters) that the function accepts, and the data types of the input arguments (parameters). In addition, in the declaration of a function, we can also specify the names of the parameters (arguments).
  • The data type of the return variable or a return object of a function.

A function declaration is the information that the compiler needs to have in order to accept the references to the declared identifier. Also, function declarations declare the name of the function and characteristics, however, function declarations do not allocate the storage memory for the function. Here are several examples of function declarations:

// declarations of several functions
int sumVariables(int a, int b);
float maxValue(float a, float b);
void functionPrintMessage(string string1);
void functionConvert(int* ,float* );

Definition of Functions in C and C++

The function definition implements (instantiates) the function. That is, the function definition at the same time declares and defines the function. That is, at the same time, it contains a declaration and the body of the function that defines the function. The function declaration also allocates the memory storage for the function. This means that the memory storage for the functions variables is reserved by defining the function. The following are possible function definitions of some functions declared previously

// definition of the function called sumVariables
int sumVariables(int a, int b)
{
int c;
return c=a+b; 
}
// definition of the function called maxValue
float maxValue(float a, float b)
{
if (a>=b)
{
return a;
}
else 
{
return b;
}
}

We can avoid separate declarations, and instead, declare and define the functions at the same time.

Important Comments Regarding Function Declarations and Definitions and Best Practices

  • A function can be declared several times in the program, however, it should be defined only once. The function declarations should be compatible. However, try to avoid this unless you use some type of function overloading.
  • In complex C or C++ projects, declarations should be placed in header files (extensions .h or .hpp), and the definitions should be placed in the source files or in implementation files (extensions .c or .cpp).
  • In smaller projects, it is a good practice to declare functions above the main function and then below the main function to define the function. This makes the code file better organized and easier to read. For example, the following code is not the best practice since the function is declared and defined at the top of the code.
// example of not the best programming practice
#include<iostream>
#include<cstdlib>
using namespace std;

// definition and declaration of the function called sumVariables
int sumVariables(int a, int b)
{
int c;
return c=a+b; 
}


int main()
{
int s=10;
int b=20;

cout<<"The sum is "<<sumVariables(s,b)<<endl;

}

Instead, a better practice is to first declare the function “int sumVariables(int a, int b)” above the main file, and then to state the definition after the main file

// good practice to state the declarations above the main file
#include<iostream>
#include<cstdlib>
using namespace std;

// declaration of the function called sumVariables
int sumVariables(int a, int b);


int main()
{
int s=10;
int b=20;

cout<<"The sum is "<<sumVariables(s,b)<<endl;

}

// definition of the function sumVariables
int sumVariables(int a, int b)
{
int c;
return c=a+b; 
}