Difference between user defined function and library function in C/C++
Library Functions
These functions are the built-in functions i.e., they are predefined in the library of the C. These are used to perform the most common operations like calculations, updation, etc. Some of the library functions are printf, scanf, sqrt, etc. To use these functions in the program the user has to use a header file associated with the corresponding function in the program.
Example
If the user has to use print the data or scan the data using an input stream then we have to use functions printf() and scanf() in C program and cin and cout in C++ program. To use these functions the user has to include #include<stdio.h> preprocessor directive in C program and #include<iostream> preprocessor directive in C++ program.
C
// C program to illustrate inbuilt function // Header file for printf function #include <stdio.h> // Driver Code int main() { // Library function printf printf ( "w3wiki!" ); return 0; } |
C++
// C++ program to illustrate inbuilt function #include <iostream> using namespace std; // Driver Code int main() { // Library function printf cout << "w3wiki!" ; return 0; } |
w3wiki!
User-defined Functions
These functions are designed by the user when they are writing any program because for every task we do not have a library of functions where their definitions are predefined. To perform according to the requirement of the user, the user has to develop some functions by itself, these functions are called user-defined functions. For such functions, the users can write their own logic according to the requirement.
Example
If we want to perform the addition of two numbers then below is the program to illustrate the addition of two numbers using a user-defined function.
C
// C program to illustrate user-defined function #include <stdio.h> // User-defined function to find the sum of a and b void findSum( int a, int b) { // Print the sum printf ( "Sum is: %d" , a + b); } // Driver Code int main() { // Given two numbers int a = 3, b = 5; // Function Call findSum(a, b); return 0; } |
C++
// C++ program to illustrate user-defined function #include <iostream> using namespace std; // User-defined function to find the sum of a and b void findSum( int a, int b) { // Print the sum cout << "Sum is: " << a + b; } // Driver Code int main() { // Given two numbers int a = 3, b = 5; // Function Call findSum(a, b); return 0; } |
Sum is: 8
Difference between User-Defined Function and Library Function
Tabular Representation to illustrate the difference between the user-defined function and the library function in C/C++:
S.No. |
User-Defined Functions |
Library Functions |
---|---|---|
1 |
These functions are not predefined in the Compiler. | These functions are predefined in the compiler of C language. |
2 |
These functions are created by users as per their own requirements. | These functions are not created by users as their own. |
3 |
User-defined functions are not stored in library files. | Library Functions are stored in a special library file. |
4 |
There is no such kind of requirement to add a particular library. | If the user wants to use a particular library function then the user has to add the particular library of that function in the header file of the program. |
5 |
Execution of the program begins from the user-define function. | Execution of the program does not begin from the library function. |
6 |
Example: sum(), fact(),…etc. | Example: printf(), scanf(), sqrt(),…etc. |