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;
}


Output

Sum is: 8

Difference between user defined function and library function in C/C++

Similar Reads

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....

User-defined Functions

...

Difference between User-Defined Function and Library Function

...