/* */ Click to Join Live Class with Shankar sir Call 9798158723

Categories of function in C++ ?

  1. function with no parameters and no return values
  2. function with no parameters and return values
  3. function with parameters and no return values
  4. functions with parameters and return values

function with no parameters and no return values

There is no data transfer between the calling function and called function. So, calling function cannot send values and hence, called function cannot receieve the data. This is also called "void functions with no parameters".


Q.) W.A.P for function with no parameters and no return values.
# include < iostream>
using namespace std;

void prime();

int main()
{
    // No argument is passed to prime()
    prime();
    return 0;
}

// Return type of function is void because value is not returned.
void prime()
{

    int num, i, flag = 0;

    cout << "Enter a positive integer enter to check: ";
    cin >> num;

    for(i = 2; i <= num/2; ++i)
    {
        if(num % i == 0)
        {
            flag = 1; 
            break;
        }
    }

    if (flag == 1)
    {
        cout << num << " is not a prime number.";
    }
    else
    {
        cout << num << " is a prime number.";
    }
}

function with parameters and return values

there is data transfer between the calling function and called function. When parameters are passed, the called function can receieve values from the calling function. When the function returns a value, the calling function can receive a value from the called function.

Q.) W.A.P for function with parameters and return values..
#include < iostream>
using namespace std;

int prime(int n);

int main()
{
    int num, flag = 0;
    cout << "Enter positive integer to check: ";
    cin >> num;

    // Argument num is passed to check() function
    flag = prime(num);

    if(flag == 1)
        cout << num << " is not a prime number.";
    else
        cout<< num << " is a prime number.";
    return 0;
}

/* This function returns integer value.  */
int prime(int n)
{
    int i;
    for(i = 2; i <= n/2; ++i)
    {
        if(n % i == 0)
            return 1;
    }

    return 0;
}

function with parameters and no return values

There is data transfer from the calling function to the called function using parameters. But, there is no data transfer from called function to the calling function.

Q.) W.A.P for function with parameters and return values..
#include < iostream>
#include < sstream>

using namespace std;
void pyramid( int n ) {
   for( int i = 1; i <= n; i++ ) {
      for( int j = 1; j <= n - i; j++ ) {
         cout << " ";
      }
      for( int j = 1; j <= i; j++ ) {
         cout << "* ";
      }
      cout << endl;
   }
}

int main()
{
   pyramid( 15 );
}