Type of user-defined function in C++
×

Type of user-defined function in C++

5005

Coding tag provides you the detail explanation about user-defined function in C++ with the help of examples. Usage of functions permits the users to solve a single problem with different approaches.

A user-defined function can be divided on the basis of its argument into four type's i.e.

# Function with no argument and no return value
# Function with the Argument passed and a return value
# Function with the argument but no return value
# Function with no argument but the return value

1) Function with no argument and no return value
When we make any function with no arguments and no return value, it neither receives any data from the calling function nor returns a value. In other words, we can say the calling function does not get any value from the called function.

Syntax for function with no argument and no return value is as under:

void function();
function();
void function()
{
any required statements;
}
Program for no argument and no return value

#include<iostream>
using namespace std;
void primeno();
int main()
{
// No argument is passed to primeno()
primeno();
return 0;
}
// Return type is void
void primeno()
{
int number, j, flag = 0;
cout << "Enter any integer and press enter to check: ";
cin >> number;
for(j = 2; j <= number/2; ++j)
{
if(number % j == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout << number << " is not a prime number.";
}
else
{
cout << number << " is a prime number.";
}
}
Result: 6

Output:
Enter any integer and press enter to check: 6 is not a prime number.

2) The Argument passed and a return value
In C++ programming language, argument represents the data that is passed to the function at the time of calling and some value is returned. This is defined by the following example

Example to illustrate the function passed with argument and a return value

#include<iostream>
using namespace std;
int prime(int m);
int main()
{
int number, flag = 0;
cout << "Enter integer number for checking purpose: ";
cin >> number;
// Argument number is passed
flag = primeno(number);
if(flag == 1)
cout << number << "entered by user is not a prime number.";
else
cout<< number << "entered by user is a prime number.";
return 0;
}
/* primeno function returns integer value. */
int primeno(int m)
{
int j;
for(j = 3; j <= m/2; ++j)
{
if(m % j == 0)
return 1;
}
return 0;
}

Output:
Enter integer number for checking purpose: 1481201936 entered by user is not a prime number.

3) Function with the argument but no return value

Example

#include <iostream>
using namespace std;
void primeno(int m);
int main()
{
int number;
cout << "Enter the integer value for checking purpose: ";
cin >> number;
// Argument number is passed to primeno() function
primeno(number);
return 0;
}
// return type of primeno function is void. */
void primeno(int m)
{
int j, flag = 0;
for (j = 2; j <= m/2; ++j)
{
if (m%j == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout << m << " that is entered by the user is not a prime number.";
}
else {
cout << m << " that is entered by the user is a prime number.";
}
}
Input
5

Output
Enter the integer value for checking purpose: 5 that is entered by the user is a prime number.

Function with no argument but the return value
In this, the main function called the function with no argument, which is further explained in the program and the return type of the primeno()  is int.

Example:

#include <iostream>
using namespace std;
int primeno();
int main()
{
int number, j, flag = 0;
// There is not any argument that is passed to primeno() function
number = primeno();
for (j = 2; j <= number/2; ++j)
{
if (number%j == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout<<number<<"i.e. the number entered by user which is not a prime number.";
}
else
{
cout<<number<<"i.e. the number entered by user which is a prime number.";
}
return 0;
}
// Return type of primeno function is int
int primeno()
{
int m;
cout<<("Enter any integer value for checking purpose: ");
cin >> m;
return m;
}
Input
5

Output
Enter any integer value for checking purpose: 5i.e. the number entered by user which is a prime number.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments