functions is a block of code that performs a specific task in c++.
There are two types of functions in c++ :
- Standard Library Functions: Predefined in C++
- User-defined Function: Created by users
User-defined Function in c++ :
C++ allows the programmer to define their own function.
A user-defined function groups code to perform a specific task and that group of code is given a name (identifier).
When the function is invoked from any part of the program, it all executes the codes defined in the body of the function.
The syntax to declare a function in c++ is :
returnType functionName (parameter1, parameter2,...) { // function body } Here's an example of a function declaration // function declaration void hello() { cout << "Hello World"; }
the name of the function is hello().
the return type of the function is void.
the empty parentheses mean it doesn’t have any parameters.
the function body is written inside {}.
User-defined Function Types
- Function with no argument and no return value
- Function with no argument but the return value
- Function with argument but no return value
- Function with argument and return value
Also read this :- friend function in c++
1) Function with no argument 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.
#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.";  } }
2) Function with no argument but the return value
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 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; }
3) Function with argument but no return value
In this program, we use the same function Number_prime() to explain the code as in the name function with the 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";  } }
4) Function with argument and return value
In this type, the function is passed with an 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; }