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);
}





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.