Types of Function in C ! Library Function in C ! User Defined Function In C ! Function Definition
Categories: C Programming language
- Library Function
- User Defined Function
Library Function in C
C provides library functions for performing some operations. These functions are present in the c library and they are predefined for example. Sqrt() is a mathematical library function which is used for finding the square root of any number The function scanf and printf() are input and output library function similarly we have strcmp() and strlen() for string manipulations.
To use a library function we have to include some header file using the preprocessor directive #include . for example : to use input and output function like printf() and scanf() we have to include stdio.h, for math library function we have to include math.h for string library string.h should be included .
User Defined Function In C
A user can create their own functions for performing any specific task of program. These types are called user defined function. to create and use these function we have to know these 3 things.
- Function Declaration
- Function Definition
- Function Call
Function Definition
The function definition consists of the whole description and code of a function. It tells that what the function is doing and what are the input output for that. A function is called by simply writing the name of the function followed by the argument list inside the parenthesis.
A function definition have two parts :
- Function Header
- Function Body
Structure of a Function
There are two main parts of the function. The function header and the function body.
int sum(int x, int y)
{
int ans = 0; //holds the answer that will be returned
ans = x + y; //calculate the sum
return ans //return the answer
}
Function Header
The first line of the above code is called Function Header.
int int x, int y)
It has three parts
- The name of the function i.e. sum
- The parameters of the function enclosed in parenthesis
- Return value type i.e. int
Function Body
What ever is written with in { } in the above example is the body of the function.
Some properties of Functions
- Every function has a unique name. This name is used to call function from main() function.
- A function performs a specific task.
- A function returns a value to the calling program.
Advantages of using Functions in C
- Functions has top down programming model. In this style of programming, the high level logic of the overall problem is solved first while the details of each lower level functions is solved later.
- A C programmer can use function written by others
- Debugging is easier in function
- It is easier to understand the logic involved in the program
- Testing is easier
Why we use Function
- Writing function avoids rewriting the same code over and over. Suppose we have a section of code that is calculating the area of triangle, if later we want to calculate the area of other triangle then we don't need to write the code again.
- By using function it becomes easier to write programs in c language.