
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Different Types of Functions in C Programming
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