Types of functions in C
Types of functions in C language
There are two types of functions in C
C library functions |
C User-defined functions |
1. C library functions
C Library functions are inbuilt functions in C programming.
Program for library functions
Square root using sqrt() function in C language
#include <stdio.h> #include <math.h> int main() { float num, root; printf("Enter a no: "); scanf("%f", &num); root = sqrt(num); printf("Sqroot of %.2f = %.2f", num, root); return 0; }
Output
Enter a no: 12 Sqroot of 12.00 = 3.46
Advantages of Using C library functions
1. Library functions are simple because they work.
2. The functions are optimized for performance
3. It saves considerable development time
4. The functions are portable
2. C User-defined functions
C allows defining functions according to your need these functions are known as user-defined functions.
For example:
Suppose, you need to create a square and color then you can create two functions
creatsqu() function
color() function
How does the user-defined function work in C?
#include <stdio.h> void functionName() { ... .. ... ... .. ... } int main() { ... .. ... ... .. ... functionName(); ... .. ... ... .. ... }
Program for user-defined function in c language
#include <stdio.h> int addNo(int p, int q); // function prototype int main() { int n1,n2,sum; printf("Enters two no: "); scanf("%d %d",&n1,&n2); sum = addNo(n1, n2); // function call printf("sum = %d",sum); return 0; } int addNo(int a, int b) // function definition { int result; result = p+q; return result; // return statement }
Output
Enters two no: 10 20 sum = 30
Advantages of user-defined function
- Used the reusable codes in other programs
- A large program is divided into smaller modules so, a large project can be divided into many programmers.
Also read, C-Booleans