0

0

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

WBOY

WBOY

发布时间:2023-09-05 14:57:15

|

1333人浏览过

|

来源于tutorialspoint

转载

创建一个带有参数但没有返回值的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 (  argument1,  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 
    #include 
    
    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.

    crmeb电商系统
    crmeb电商系统

    CRMEB 是基于Thinkphp5基础开发的以会员为中心的电商系统,开源版微信公众号商城和小程序商城数据同步,带积分、优惠券、秒杀、砍价、分销等功能,更是一套方便二次开发的商城框架(后台封装了独有快速创建表单功能,无需写表单页面、快速创建数据搜索和数据列表页、导出表格、系统权限配置控制每一个控制器方法、系统参数配置、数据字典、组合数据等)

    下载

    立即学习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 
    #include 
    
    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++速学教程(入门到精通)
    c++速学教程(入门到精通)

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

    下载

    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    相关专题

    更多
    python中print函数的用法
    python中print函数的用法

    python中print函数的语法是“print(value1, value2, ..., sep=' ', end=' ', file=sys.stdout, flush=False)”。本专题为大家提供print相关的文章、下载、课程内容,供大家免费下载体验。

    186

    2023.09.27

    typedef和define区别
    typedef和define区别

    typedef和define区别在类型检查、作用范围、可读性、错误处理和内存占用等。本专题为大家提供typedef和define相关的文章、下载、课程内容,供大家免费下载体验。

    108

    2023.09.26

    define的用法
    define的用法

    define用法:1、定义常量;2、定义函数宏:3、定义条件编译;4、定义多行宏。更多关于define的用法的内容,大家可以阅读本专题下的文章。

    336

    2023.10.11

    while的用法
    while的用法

    while的用法是“while 条件: 代码块”,条件是一个表达式,当条件为真时,执行代码块,然后再次判断条件是否为真,如果为真则继续执行代码块,直到条件为假为止。本专题为大家提供while相关的文章、下载、课程内容,供大家免费下载体验。

    93

    2023.09.25

    javascriptvoid(o)怎么解决
    javascriptvoid(o)怎么解决

    javascriptvoid(o)的解决办法:1、检查语法错误;2、确保正确的执行环境;3、检查其他代码的冲突;4、使用事件委托;5、使用其他绑定方式;6、检查外部资源等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

    176

    2023.11.23

    java中void的含义
    java中void的含义

    本专题整合了Java中void的相关内容,阅读专题下面的文章了解更多详细内容。

    98

    2025.11.27

    function是什么
    function是什么

    function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果。本专题为大家提供function是什么的相关的文章、下载、课程内容,供大家免费下载体验。

    481

    2023.08.04

    js函数function用法
    js函数function用法

    js函数function用法有:1、声明函数;2、调用函数;3、函数参数;4、函数返回值;5、匿名函数;6、函数作为参数;7、函数作用域;8、递归函数。本专题提供js函数function用法的相关文章内容,大家可以免费阅读。

    163

    2023.10.07

    c++ 根号
    c++ 根号

    本专题整合了c++根号相关教程,阅读专题下面的文章了解更多详细内容。

    45

    2026.01.23

    热门下载

    更多
    网站特效
    /
    网站源码
    /
    网站素材
    /
    前端模板

    相关下载

    更多

    精品课程

    更多
    相关推荐
    /
    热门推荐
    /
    最新课程
    C# 教程
    C# 教程

    共94课时 | 7.5万人学习

    C 教程
    C 教程

    共75课时 | 4.2万人学习

    C++教程
    C++教程

    共115课时 | 13.7万人学习

    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送

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