What are the different types of functions in C Programming?

CServer Side ProgrammingProgramming

Functions are broadly classified into two types which are as follows −

  • predefined functions
  • user defined functions

Predefined (or) library functions

  • These functions are already defined in the system libraries.

  • Programmer can reuse the existing code in the system libraries which is helpful to write error free code.

  • User must be aware of syntax of the function.

For instance, sqrt() function is available in math.h library and its usage is y= sqrt (x), where x= number must be positive.

If x value is 25, i.e., y = sqrt (25) then ‘y’ = 5.

In the same way, printf() is available in stdio.h library and clrscr() is available in conio.h library.

Program

#include<stdio.h>
#include<conio.h>
#include<math.h>
main (){
   int x,y;
   clrscr ();
   printf (“enter a positive number”);
   scanf (“ %d”, &x)
   y = sqrt(x);
   printf(“squareroot = %d”, y);
   getch();
}

Output

Enter a positive number 25
Squareroot = 5

User defined functions

  • These functions must be defined by the programmer or user.

  • Programmer has to write the coding for such functions and test them properly before using them.

  • The syntax of the function is given by the user so there is no need to include any header files.

For example, main(), swap(), sum(), etc., are some of the user defined functions.

Example

#include<stdio.h>
#include<conio.h>
main (){
   int sum (int, int);
   int a, b, c;
   printf (“enter 2 numbers”);
   scanf (“ %d %d”, &a ,&b)
   c = sum (a,b);
   printf(“sum = %d”, c);
   getch();
}
int sum (int a, int b){
   int c;
   c=a+b;
   return c;
}

Output

Enter 2 numbers 10 20
Sum = 30
raja
Published on 08-Mar-2021 12:51:33
Advertisements