C Control StatementsC FunctionsC ArrayC PointerC Dynamic MemoryC StringC MathC StructureC UnionC File HandlingC PreprocessorC Command LineC Misc |
Functions in cA function is a self container or subprogram that perform some specific task. C programs consists of one or more then one functions. Everything will be happen inside the function in c programming. Advantage of using Function1. There generally difficult problem is divided into sub problems and solve. The c program is divided into number of functions and each function performs specific task that means the program or work divide in modules. 2. And when the specific code is used more then once and use at different places we use functions. 3. The program becomes easy understandable, modifiable and the easy to debug and testing. it becomes easy to write and understand what function or parts of program do work. 4 .The function can be stored in library or in header file and reusable. The C programs have to type of functions
1. Library functions Library functionsC has the facility to provides library function to performing operations.The libraries functions are present in c libraries and they are predefined. for example printf() is library function, its is used from print any string in the console which is stored in < stdio.h > header file. scanf () are used for input value from the user it is also stored in stdio.h headerfile. similar we have functions strlen() and strcmp() for string manipulations. To use any library functions we have to include corresponding headerfile using the preprocessor directive that is #include. if we want to use printf() and scanf() library function, we will include stdio.h let us take example to find the square root of number using sqrt() function
User defined functionsThe programmers can create or make their own function for doing any specific task of the program. These functions are called or known as user defined functions.So to create user defined function we should know three things .
1. function declartion take the simple example of function which includes function declaration, function definition and function calling after this, discuss the all three parts of function
Function declarationA function declaration is used to give the information about function to the compiler. so this can check the function calling so the function calling needs the information about the called function. but if definition of the function is given before the calling function then there is not need to declare the function. note: if we write the definition of any function before the main function then no need to declare that function.
Here the definition of sub() function is written before the main(), so main() knows everything of the function sub(). but at all time generally main() is placed at the top of the program and other functions are placed after it then in this case function declaration is required. when declare the function that informs the compiler about the following three things: –
1. Name of the function
|