Fusion of Engineering, Control, Coding, Machine Learning, and Science

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.

What is the Difference Between Function Declaration and Definition in C and C++ - Robotics/ML

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:

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

// 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; 
}
Exit mobile version