首页 > 后端开发 > C++ > 正文

创建一个带有参数但没有返回值的C++程序

WBOY
发布: 2023-09-05 14:57:15
转载
1270人浏览过

创建一个带有参数但没有返回值的c++程序

Functions in functional programming are used to create modular codes. We construct submodules in many applications to make our code simple to write, simple to debug, and also efficient by avoiding writing needless code repeatedly. Functions have a role in achieving these traits. Functions frequently take arguments and produce a result. It occasionally might not accept any arguments but still produce something. There are several rare circumstances when functions take a few inputs but don't return anything. This course will discuss C++ methods that accept arguments but nothing is returned.

Function with arguments but no return value

To define a such function with arguments but no return type, the return type must be void, and there must be at least one argument in the parameter list

语法

void function_name ( <type> argument1, <type> argument2, … ) {
   // function body
}
登录后复制

In such a scenario, to produce output we just print something, or performing any displaylike operations, or perform some task whole inside the function without showing any output. Let us see one of such examples and let us see the implementation in C++. In our example, we will print a star pyramid for n number of lines where n is the argument passes through the function.

Algorithm

  • Define a function pyramid(), this will take an integer n
  • 用于初始化 i := 1,当 i
  • for initialize j := 1, when j
  • 显示空白空间
  • end for
  • for initialize j := 1, when j
  • display "*"
  • end for
  • 去到新的一行
  • end for
  • 结束函数体
  • 调用 pyramid()
  • Example

    #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 );
    }
    
    登录后复制

    输出

                  * 
                 * * 
                * * * 
               * * * * 
              * * * * * 
             * * * * * * 
            * * * * * * * 
           * * * * * * * * 
          * * * * * * * * * 
         * * * * * * * * * * 
        * * * * * * * * * * * 
       * * * * * * * * * * * * 
      * * * * * * * * * * * * * 
     * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * *
    
    登录后复制

    This program, it is printing the pyramid for n lines, here n is 15. The line number is passed through the argument. Since it is printing the asterisks directly, nothing is returned. Let us see another example where we take one number and display the reverse of it using function call.

    立即学习C++免费学习笔记(深入)”;

    Algorithm

    • Define reverse() function which takes a number n
    • rev := 0
    • while n > 0, do
      • rev := (rev * 10) + (n mod 10)
      • n := floor of (n / 10)
    • end while
    • display rev
    • 结束函数
    • 使用任意数字调用reverse()函数,它将显示该数字的反转。

    Example

    #include <iostream>
    #include <sstream>
    
    using namespace std;
    void reverse( int n ) {
       int rev = 0;
       while( n > 0 ) {
          rev = (rev * 10) + n % 10;
          n = n / 10;
       }
       cout << rev << endl;
    }
    
    int main()
    {
       cout << "Reverse of 14586 is: ";
       reverse( 14586 );
       cout << "Reverse of 1250 is: ";
       reverse( 1250 );
       cout << "Reverse of 15651 is: ";
       reverse( 15651 );
    }
    
    登录后复制

    输出

    Reverse of 14586 is: 68541
    Reverse of 1250 is: 521
    Reverse of 15651 is: 15651
    
    登录后复制

    这里还是从调用函数中获取数字作为参数,然后直接显示结果,所以没有返回任何内容。

    结论

    函数用于使代码模块化和易于处理。在大多数情况下,我们使用函数来接收参数并在某些计算后返回某个值。但这不是强制性的过程。在本文中,我们讨论了如何在C++中编写一个接收参数但不返回任何内容的函数。就像在我们的第一个例子中,星星金字塔是根据输入的n行打印出来的,并直接显示结果。在第二个例子中,我们将数字作为参数传递,然后将其转换为其反向形式,并且不返回结果,而是在控制台上显示结果。

    以上就是创建一个带有参数但没有返回值的C++程序的详细内容,更多请关注php中文网其它相关文章!

    c++速学教程(入门到精通)
    c++速学教程(入门到精通)

    c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

    下载
    来源:tutorialspoint网
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    开源免费商场系统广告
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习
    PHP中文网抖音号
    发现有趣的

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号