A set of statements written to perform any task is known as function.
Syntax –
1 2 3 4 5 6 7 8 9 10 11 12 |
return_value function_name(datatype input1, datatype input2, .... datatype inputn) { statement1; statement2; statement3; -------------- statementn; } |
Example
1 2 3 4 5 6 7 8 9 10 |
void printNaturalNumber(void) /*function definition*/ { int i; printf("Natural Numbers :"); for(i=0;i<10;i++) { printf("%d, ",i); } } |
In above example function, printNaturalNumber has –
— set of statements to perform the printing of natural number.
— void input i.e no input parameter.
— void output i.e no output return.
— Function’s use increases the program readability.
— Function’s use decreases the complexity.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include<stdio.h> int main() { statement1; statement2; statement3; statement4; statement5; statement6; statement7; statement8; statement9; statement10; statement11; statement12; statement13; return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include<stdio.h> void function1(void); /*function1 Prototype Declaration*/ void function2(void); /*function2 Prototype Declaration*/ void function3(void); /*function3 Prototype Declaration*/ void function4(void); /*function4 Prototype Declaration*/ int main() { function1(); /*function1 calling*/ function2(); /*function2 calling*/ function3(); /*function3 calling*/ function4(); /*function4 calling*/ return 0; } void function1() /*function1 definition*/ { statement1; statement2; statement3; } void function2() /*function2 definition*/ { statement4; statement5; statement6; } void function3() /*function3 definition*/ { statement7; statement8; statement9; statement10; } void function4() /*function4 definition*/ { statement11; statement12; statement13; } |
So we can see in above example program is more readable and less complex with the use of functions.
In C language there are two types of function –
— Library defined functions
— User-defined function
library defined functions are standard functions defined in the standard C library. We only need to include the header file prior to use of any standard function because header file contains the prototype of
Example:
the main function is defined in the standard library and its prototype declared in stdio.h header file.
printf() ,scanf(), getch(), gets() etc. all functions are standard and defined in a standard library.
User-defined function as the name appears –“A function defined by a user/programmer”. these functions are not standard and these are the project-specific functions written by a programmer to make program modular and less complex.
1 2 3 4 5 6 7 8 9 10 |
void printNaturalNumber(void) /*function definition*/ { int i; printf("Natural Numbers :"); for(i=0;i<10;i++) { printf("%d, ",i); } } |
Above function is an user-defined function written by a programmer to print natural number.