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