Updated April 7, 2023
Introduction to C++ User-Defined Function
C++ user-defined function lets users describe their individual/own functions and procedures, functions are the building blocks of the program and its most important for modularity, code reusability whereas programmer can build a user-defined function which assists in a particular task and it makes it simple to create a function call. In a user-defined function, once the function is invoked from the program it executes the code which is defined in the body of the function. In a user-defined function, it collects with a group of code to perform a task for that code it named as an identifier.
The user-defined function makes programmer build their own functions. The most important thing behind these functions is the programmer can create applications with reusable code. Mostly the user-defined functions are with built-in functions.
Syntax
Below is the syntax:
returntype function_name(parameter-1, parameter-2,..)
{
//body of function
}
Let’s see the simple program for calling the function,
In this simple program we declared the function display(), the return type of this code is void and in the body of function written the welcome address,
For Example
void display() // function declaration
{
cout<<"Welcome"
}
To make use of this function we need to make a function call,
Program
#include <iostream>
using namespace std;
// to declare the function
void display()
{
cout << "Welcome to Programming";
}
int main() {
// function call
display();
return 0;
}
Output:
User-defined Functions Types
In user-defined function there are several types, they are
1. Functions with no parameters and no return value
Function with no parameter and with no return type which does not return value because its return type is void. In this coding, there are no parameters passed in the function Number_prime() and the function also does not return any value from the function call to the main function.
Code:
#include <iostream>
using namespace std;
void Number_prime();
int main()
{
Number_prime(); // no parameters
return 0;
}
void Number_prime() // no return type - its void
{
int number, i, flag = 0;
cout << "Enter Numer to check: ";
cin >> number;
for(i = 2; i <= number/2; ++i)
{
if(number % i == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout << number << " Not a Prime.";
}
else
{
cout << number << " its Prime Number.";
}
}
Output:
2. Functions with no parameters and with return value
In this program, Number_prime() called from the main() function without any parameters, here the return type is an integer so it returns the integer values from the user’s input when calling main() function.
Code:
#include <iostream>
using namespace std;
int Number_prime();
int main()
{
int number, i, flag = 0;
number = Number_prime();
for (i = 2; i <= number/2; ++i)
{
if (number%i == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout<<number<<" Not a Prime";
}
else
{
cout<<number<<" its Prime";
}
return 0;
}
int Number_prime() // it returns integer value
{
int n;
printf("Enter Number: ");
cin >> n;
return n;
}
Output:
3. Functions with parameters and with no return value
In this program, we using the same function Number_prime() to explain the code as in the name function with parameter the function Number_prime() will take with one integer value as its argument without any return value. Here the number will be passed to the function call Number_prime() to check whether the entered number is prime or not.
Code:
#include <iostream>
using namespace std;
void Number_prime(int n);
int main()
{
int number;
cout << "Enter Value: ";
cin >> number;
// one parameter is passed to the function Number_prime()
Number_prime(number);
return 0;
}
// return type is void, so it does not return any value
void Number_prime(int n)
{
int i, flag = 0;
for (i = 2; i <= n/2; ++i)
{
if (n%i == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout << n << " Not a Prime Number";
}
else {
cout << n << " Its Prime Number";
}
}
Output:
4. Functions with parameters and with a return value
In this type, the function is passed with argument and also it returns the value, in this program the user will input the integer value and that value is first stored in the variable and then passed to the function call to check whether the entered value is prime or not. Here the return type of Number_prime() is an integer, it either returns 1 or 0 to the main() function. If the entered value is prime then it returns 1, otherwise, it returns 0. In the main () function call the returned value is stored in the flag based on that it displays the text on the screen.
Code:
#include <iostream>
using namespace std;
int Number_prime(int n);
int main()
{
int number, flag = 0;
cout << "Enter positive integer: ";
cin >> number;
// one argument is passed to the function
flag = Number_prime(number);
if(flag == 1)
cout << number << " Not a Prime Number";
else
cout<< number << " its a Prime Number";
return 0;
}
/* it have a return type - integer */
int Number_prime(int n)
{
int i;
for(i = 2; i <= n/2; ++i)
{
if(n % i == 0)
return 1;
}
return 0;
}
Output:
Benefits of C++ User-Defined Functions
- By using user-defined function it supports code reusability, we can use the code several times and by declaring once.
- User-defined functions reduce the complexity of huge programs and optimize the code.
- User-defined function helps user to build programs easily because every task is divided into several functions.
- User-defined function enhances readability.
Conclusion
Hope this article helps you to understand the user-defined functions in C++ with several examples. In this article, we have seen the usages of functions and different types available in user-defined functions.
Recommended Articles
This is a guide to C++ User-Defined Function. Here we discuss Introduction, syntax, User-defined Functions Types examples with code implementation. You may also have a look at the following articles to learn more –