Compare the Difference Between Similar Terms

Difference Between Declaration and Definition in C

The key difference between declaration and definition in C is that declaration in C tells the compiler about the function name, return type and parameters while definition in C contains the actual implementation of the function. That is, declaration provides information about the function to the compiler whereas, definition contains the actual statements of the function to perform a specific task.

C is a general purpose, structured programming language. It uses control structures such as if/else, repetitions such as for loop, while loop and functions. A function is a set of statement that helps to perform a certain task over and over again. Furthermore, it is possible to call the functions from the main function. After executing the last statement of the function, the control passes back to the main function. This article discusses the declaration and definition of functions in C and compares the difference between them. The function definition specifies what the function does, and declaration specifies what goes to the function; it is a prototype.

CONTENTS

1. Overview and Key Difference
2. What is Declaration in C
3. What is Definition in C
4. Side by Side Comparison – Declaration vs Definition in C in Tabular Form
5. Summary

What is Declaration in C?

Declaration provides information about the function to the compiler. The syntax for the declaration is as follows.

return_ type function_name (parameter list);

Assume a function that calculates the sum of two integers. The declaration is as follows.

int sum (int num1, int num2);

The name of the function is sum, and the parameters are two integers which are num1 and num2. This function returns an integer. The complete statement ends with a semicolon.

It is not necessary to include the names of the parameters in the declaration. Therefore, it is also possible to mention only the data type as follows. Following is a valid declaration.

int sum (int, int);

What is Definition in C?

Definition contains the actual statements of the function to perform a specific task. The syntax is as follows.

return_type  function_name (parameter list){

// function statements

}

Function name helps to identify the function. When invoking a function, values pass to that function. These values copies to the parameters. The parameter list can contain a one parameter or number of parameters. And these parameters have a data type and a name. Moreover, there can be functions without any parameter as well.

The statements of the function are inside the curly braces. It is the function body. After executing the function, it will return a value. The return type depends on the return value. If the function returns an integer, the return type is int. If the function returns a double, then the return type is double etc.

Refer the below code with declaration and definition of a function.

Figure 01: Program to Calculate the Summation of Two Numbers

According to the above program, line 3 displays the declaration. It tells the compiler about the function name, parameters etc. In the main function, two values are taken from the keyboard, and they are stored into the variable ‘a’ and ‘b’. In line 12, these values are passed to the function called sum. This ‘a’ and ‘b’ are arguments.

In line 16, the sum function executes. It copies the value a to num1 and value b to num2. This function returns the summation and that value stores to the variable ‘ans’ (line 12). Finally, the answer prints to the screen. In brief, line 3 shows the declaration while line 16 to 18 displays the definition.

What is the Difference Between Declaration and Definition in C?

Declaration is a prototype that specifies the function name and type signature such as data types, return types and parameters but omits the function body. Definition specifies the function name and type signatures such as data types, return types and parameters, and it includes the function body. Declaration tells the compiler about the function name and how to call it. On the other hand, definition contains the actual implementation of the function. It describes the task of the function.

Summary – Declaration vs Definition in C

The difference between declaration and definition in C is that declaration in C tells the compiler about the function name, return type and parameters while definition in C contains the actual implementation of the function.

Reference:

1.Tutorials Point. “C Functions.”  Tutorials Point, 19 Mar. 2018. Available here  
2.“Function Prototype.” Wikipedia, Wikimedia Foundation, 28 May 2018. Available here