Home >Backend Development >C++ >C++ program creates a function with no parameters and no return value

C++ program creates a function with no parameters and no return value

WBOY
WBOYforward
2023-09-02 16:37:061488browse

C++ program creates a function with no parameters and no return value

In programming languages, functions are used to modularize code. In many applications, we create submodules to make our code easy to write, easy to debug, and optimize by repeatedly rejecting unnecessary code. To implement these functions, functions appear on the screen. In many cases, functions accept parameters and return something. Sometimes it may not accept any parameters but return something. There are also special cases where functions neither accept any parameters nor return anything. In this tutorial, we will introduce such a function in C without parameters and return value.

Function without parameters and without return type

To define a function without parameters and return type, the return type must be void, the parameter list can be empty, or we can write void there. The syntax is as follows.

grammar

void function_name ( ) {
   // function body
}

grammar

void function_name ( void ) {
   // function body
}

In a scenario like this, where we just print something, or perform any operation like display, or perform some tasks inside the function, this situation is suitable for this type of function. Let's look at an example of this and see the implementation in C. In our first example, we will print a fixed 10-row star pyramid.

algorithm

  • Define a function pyramid(), this does not require anything
  • For initialization i := 1, when i
  • Used to initialize j := 1, when j
  • Display blank
  • Finish
  • Used to initialize j := 1. When j
  • show"*"
  • Finish
  • Go to new line
  • Finish
  • 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

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

    The program prints only 10 sizes of pyramids. Since the size is fixed, it takes no arguments, and since it prints the asterisk directly, nothing is returned. Let's look at another example like Star Pyramid, which takes input from the user, but we also don't pass any parameters and the function doesn't return anything.

    algorithm

    • Define a function pyramid(), this does not require anything
    • Use n as user input
    • For initialization i := 1, when i
    • For initialization j := 1, when j
    • Display blank
  • Finish
  • Used to initialize j := 1, when j
  • show"*"
  • Finish
  • Change to a new line
  • Finish
  • 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 use the cin method to obtain user input. This solution requires no additional parameter passing.

    in conclusion

    Functions are used to make the code modular and easy to handle. In most cases, we use functions that accept parameters and return some value after some calculations. But this is not a mandatory process. In this article, we discussed how to write a function in C that takes no parameters and returns nothing. We can use this type of function when a certain task is predefined. Like in our first example, the star pyramid only has 10 rows, so no additional input is required. In the second example, we take the line number as input, but not as an input parameter. We get the input directly from the user and store it in a local variable inside this function and then use it in the loop.

    The above is the detailed content of C++ program creates a function with no parameters and no return value. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete