Software Developer
C++ person-defined characteristic we could customers describe their individual/personal capabilities and procedures, capabilities are the building blocks of the program, and it's most essential for modularity, code reusability while programmer can build a consumer-described feature which assists in a specific venture and it makes it easy to create a character name. In a person-described function, once the feature is invoked from the program it executes the code that is described within the body of the function. In a consumer-described function, it collects with a collection of code to perform an undertaking for that code it named as an identifier.
The user-defined function makes programmer build their 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.
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,
Void display() // function declaration
{
cout<<"Welcome"
}
to make use of this function we need to make a function call,
#include <iostream>
using namespace std;
// to declare the function
void display()
{
cout << "Welcome to Programming";
}
int main() {
// function call
display();
return 0;
}
In user-defined function there are several types, they are
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.
# 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.";
}
}
In this program, Number_prime() is 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 the main() function.
#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;
}
In this program, we use 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.
#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";
}
}
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.
#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;
}