Function In C Programming

-Advertisement-

A function is a self-contained program segment that carries out some specific well-defined tasks. Main is the basic and simple example of a function. A function is the combination of a number of statements.

Where, statement is an instruction to perform one particular operation. This means statement causes the computer to carry out some action in order to solve certain problems.

The advantages of making program modular using function are as follows:

1. The use of the function makes the program logically clear and easy to debug and understand.
2. The use of function makes easier to modify a program.
3. It enables a programmer to build a customized library of frequently used routines.
4. A function can be called any number of times with different parameters. so it avoids the need of repeated programs.

There are two types of function in C programming language and they are:

1. Library function
2. User-defined function

1. Library function/ System defined function/ Pre-Defined Functions

A library function is the collection of functions which are grouped together in header in order to reuse those function multiple times without coding. System defined function can’t be modified, it can only read and can be used.

Some Example of library function are printf(),  scanf(), strcat()

Importance of library function

  1. They are used for mathematical operations and calculations (like evaluating sines and cosines).
  2. The use of library function avoids rewriting codes again and again for just one specific operation.

2. User-defined function

Functions that are defined by the user according to their requirements are known as User Defined Function.

The following example has a user defined function reverse() in order to reverse a given number.

Wap To Reverse A Given Number Using C

#include<stdio.h>
int reverse (int num)
{
int rev= 0, r;
while(num>0)
{
r= num%10;
rev= rev*10+r;
num=num/10;
}
return rev;
}
int main()
{
int n;
printf("enter the number:");
scanf("%d", &n);
printf("\n reverse number is: %d", reverse(n));
return 0;
}

Purpose of user defined function

1. It facilities top-down modular programming
2. It is a design so that it can be called and use whenever required
3. The use of user-defined function makes easier to debug, understand and test program.

Also Read: Recursion In C Programming

-Advertisement-

LEAVE A REPLY

Please enter your comment!
Please enter your name here