Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-09-02 16:37:06
forward
1418 people have browsed it

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
}
Copy after login

grammar

void function_name ( void ) {
   // function body
}
Copy after login

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 <= 10, update (increase i 1), execute −
    • Used to initialize j := 1, when j <= 10 - i, update (j plus 1), execute. <= 10 - i 时,更新(j 加 1),执行。
      • Display blank
    • Finish
    • Used to initialize j := 1. When j <= i, update (j increases by 1) and execute. <= i时,更新(j增加1),执行。
      • 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();
}
Copy after login

Output

         * 
        * * 
       * * * 
      * * * * 
     * * * * * 
    * * * * * * 
   * * * * * * * 
  * * * * * * * * 
 * * * * * * * * * 
* * * * * * * * * *
Copy after login

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 <= n, update (increase i by 1), execute - <= n 时,更新(将 i 增加 1),执行 -
    • For initialization j := 1, when j <= n - i, update (increase j 1), execute
      • Display blank
    • Finish
    • Used to initialize j := 1, when j <= i, update (j increases by 1), execute <= i时,更新(j增加1),执行
      • 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();
}
Copy after login

Output

Enter line numbers: 18
                 * 
                * * 
               * * * 
              * * * * 
             * * * * * 
            * * * * * * 
           * * * * * * * 
          * * * * * * * * 
         * * * * * * * * * 
        * * * * * * * * * * 
       * * * * * * * * * * * 
      * * * * * * * * * * * * 
     * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * 
   * * * * * * * * * * * * * * * 
  * * * * * * * * * * * * * * * * 
 * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * *
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!