C Programming language

adplus-dvertising
Types of Function in C
Previous Home Next
  • 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.

  1. Function Declaration
  2. Function Definition
  3. 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 sum( int x, int y)

It has three parts

  1. The name of the function i.e. sum
  2. The parameters of the function enclosed in parenthesis
  3. 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

  1. 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.
  2. By using function it becomes easier to write programs in c language.
Previous Home Next