Functions are easy to use. They allow complicated programs to be break into small blocks, each of which is easier to write, read, and maintain. We have already used the function main and scanf, printf . where main is used-defined function and scanf and printf are library functions. main() is a function from there program execution starts.
The functions defined by user itself is known as user defined function.Function used from external files or library known as library functions. printf , scanf , getchar(), gets(), puts() are library functions.
Function has following layout.
1. Function returning value
return-type function-name( argument-list-if-you-want){
statements;
statements;
return return-value;
}
2. Function no return-value
void function-name( arguments-list-if-you-want){
statements;
statements;
}
Return type :
Return type refers to the type of value which is returned by function.
eq. if function return integer value then return type should be int.
Example :
A function to add 2 integer values.
1 2 3 4 5 6 7 8 9 10 |
#include<stdio.h> int add(int a,int b){ return a+b; } int main(){ int c; c=add(3,5); printf(" %d ",c); return 0; } |
above code will print 8 as output.