Many of us use the term ‘Declaration’ when referring to variables. However, what is the difference between declaration and definition of a function in C Programming? The answer to the question is simple, the Declaration of a function is the information about the function. A declaration says that a function is declared and what are the inputs and outputs. It also tells about the return type of the function. In contrast, a definition describes the function itself. A definition contains the information about the function parameters, return type, and the arguments passed to the function.
There is no confusion that a function is a variable, but the difference lies in the fact that a variable is a data element that stores values and function is a block of code that performs certain action.
When we write a program, we must create a function first and then define the function. In the case of functions in C Programming, we write the definition in the header of the program and declare the function in the implementation section.
To conclude, a function is a block of code that executes on a computer, and a variable is a value that stores a certain value. In addition to this, a variable is a data element that stores a value, but a function is a block of code that executes.
The difference between the Declaration and the Definition
Definition:
A definition is a definition of a word, phrase, sentence, etc. It is the explanation of the meaning of a word.
- The process of defining a function in C programming is also very simple. You need to use the function keyword before the function’s name. Like int function_name();
- Definition of a variable is the process of defining the variable. It tells the compiler that the variable is created.
- In the declaration, the function is defined, whereas in the definition, the function is created.
- A good example of a definition is:
> The definition of ‘definition’ is ‘a description of the meaning of a word’.
Here we can see that the definition is very clear and simple. It is a very important thing for a writer to understand the concept of a definition. - We usually use the definition of a thing in a sentence or in a paragraph.
> The definition of a ‘function’ is ‘a procedure or action that can be performed on a thing’.
But, here we can see that the definition is in the sentence. We can’t see the definition directly.
Declaration:
It is the statement or the expression that is used to declare a new thing. For example, you can see a declaration on the front page of a website. It is the place where you can see the title of the website.
- In the declaration, the function name, parameters, and return type are declared, whereas in the definition, the function name, parameters, and return type are defined.
- Declaration is nothing but it is the process of telling to the compiler that a particular variable is created. The process of defining a function is nothing but it is the process of telling the compiler to create a function that has a specific name.
- The process of declaring a variable in C programming is very simple. You just need to add the ‘int’ in front of the word ‘var’. Like int var1;.
- Declaration of a variable is the process of declaring the variable. Variables cannot be created without the declaration of the variable.
- A good example of a declaration is:
> The declaration of ‘Grammarly’ is a company that provides grammar checkers and proofreading tools to the students. - This is a very short and simple definition. Here the main thing that you have to keep in mind is that the Declaration of a thing is not the same as a definition. The definition is much longer than the declaration.
- We usually use the Declaration in a sentence.
> The declaration of a ‘function’ is ‘an operation that is performed on a thing’.
This is the same thing as the definition. We can’t see the definition directly.
A function is created in the form of a declaration and is used to define the function. A function declaration consists of a function name, parameters, return type, and a list of statements. These statements are enclosed by braces and a semicolon.
Example
// Declare a function
void printName(char *name);
// Print the name of a person
printName("Coddy");
Declaration
function printName(name)
{
printf("Your name is %s\n", name);
}
Definition
function printName(name)
{
printf("Your name is %s\n", name);
}
The above example shows how the function is defined. In the above example, the printName function is declared and defined.
Conclusion:
There is a big difference between the declaration and the definition of a function and variable in C programming. The definition is the process of telling the compiler that a particular function is created. The function’s name, return type and arguments are also specified in the definition of a function. I hope you learned the difference between the Declaration and the Definition. If you still have any questions then you can comment down below.