C++ Program to create a function without argument and without a return value


Functions in programming languages are used to make codes modular. In many applications, we create sub-modules to make our code easy to write, easy to debug, and also optimized by rejecting unnecessary code again and again. To achieve these features, functions come into the picture. In many cases, functions take arguments and return something. Sometimes it may not take any argument but returns something. Some special cases are also there when the functions do not take any arguments and do not return anything. In this tutorial, we shall cover such functions without argument and return value in C++.

Function without argument and without return type

To define a such function with no argument and no return type, the return type must be void and the argument list can either be empty or we can write void there. The syntax is shown in the following boxes.

Syntax

void function_name ( ) {
   // function body
}

Syntax

void function_name ( void ) {
   // function body
}

In such a scenario, where we just print something, or performing any display-like operations, or perform some task whole inside the function, such cases are suitable for this type of function. Let us see a such case and let us see the implementation in C++. In our first example, we will print a star pyramid for fixed 10 lines.

Algorithm

  • Define a function pyramid(), this will take nothing
  • for initialize i := 1, when i <= 10, update (increase i by 1), do −
    • for initialize j := 1, when j <= 10 - i, update (increase j by 1), do.
      • display blank space
    • end for
    • for initialize j := 1, when j <= i, update (increase j by 1), do.
      • display "* "
    • end for
    • go to the new line
  • end for
  • End function body
  • Call pyramid()

Example

#include <iostream> #include <sstream> using namespace std; void pyramid( ) { for( int i = 1; i <= 10; i++ ) { for( int j = 1; j <= 10 - i; j++ ) { cout << " "; } for( int j = 1; j <= i; j++ ) { cout << "* "; } cout << endl; } } int main() { pyramid(); }

Output

         * 
        * * 
       * * * 
      * * * * 
     * * * * * 
    * * * * * * 
   * * * * * * * 
  * * * * * * * * 
 * * * * * * * * * 
* * * * * * * * * *

This program, it is printing the pyramid for 10 sizes only. As the size is fixed, it does not take any parameter, and since it is printing the asterisks directly, nothing is returned. Let us see another example for the similar star pyramid with taking inputs from the user, but there also we do not pass any argument and the function will not return anything.

Algorithm

  • Define a function pyramid(), this will take nothing
  • Take n as input from the user
  • for initialize i := 1, when i <= n, update (increase i by 1), do −
    • for initialize j := 1, when j <= n - i, update (increase j by 1), do
      • display blank space
    • end for
    • for initialize j := 1, when j <= i, update (increase j by 1), do
      • display "* "
    • end for
    • go to a new line
  • end for
  • End function body
  • Call pyramid()

Example

#include <iostream> #include <sstream> using namespace std; void pyramid( void ) { int n; cout << "Enter line numbers: "; cin >> 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(); }

Output

Enter line numbers: 18
                 * 
                * * 
               * * * 
              * * * * 
             * * * * * 
            * * * * * * 
           * * * * * * * 
          * * * * * * * * 
         * * * * * * * * * 
        * * * * * * * * * * 
       * * * * * * * * * * * 
      * * * * * * * * * * * * 
     * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * 
   * * * * * * * * * * * * * * * 
  * * * * * * * * * * * * * * * * 
 * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * *

Here we are taking input from the user using the cin method. No extra argument passing is required for this solution.

Conclusion

Functions are used to make code modular and easy to handle. In most cases, we use functions to take arguments and return some value after certain computation. But this is not a mandatory process. In this article, we discussed how a function can be written in C++ which does not take any argument and also does not return anything. When some task is predefined, we can use this kind of function. Like in our first example, the star pyramid is for only 10 lines. So no extra input is taken. In the second example, we are taking the line number as input, but not as an input argument. We are directly taking input from the user and storing it into a local variable inside this function then using it in the loop.

Updated on: 19-Oct-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements