Home »
Programming Tips & Tricks »
C - Tips & Tricks
Correct way to declare and define a function in C
By: IncludeHelp, on 01 MAR 2017
Learn: How to declare and define the functions in C language, what is the correct and incorrect way to declare and define functions?
A function should be declared before the main() function (in the global section), so that we can use it within the main() or other user define function.
It is a wrong practice to declare and define the function together, like this:
#include <stdio.h>
/*Declaration & Definition*/
int add(int a,int b){
return (a+b);
}
int main()
{
/*function calling*/
printf("%d\n",add(10,20));
return 0;
}
Correct method
You should declare a function before the main() separately and define the function after the main(), like this:
#include <stdio.h>
/*Function declaration*/
int add(int a,int b);
int main()
{
/*function calling*/
printf("%d\n",add(10,20));
return 0;
}
/*Function definition*/
int add(int a,int b){
return (a+b);
}