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 ( <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 <= n 时,更新(增加 i 1),执行 −
    • for initialize j := 1, when j <= n - i, update (increase j by 1), do −
      • 显示空白空间
    • end for
    • for initialize j := 1, when j <= i, update (increase j by 1), do −
      • 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.

Lovart
Lovart

全球首个AI设计智能体

下载

立即学习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
  • 结束函数
  • <li>使用任意数字调用reverse()函数,它将显示该数字的反转。</li>

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

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

下载

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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

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

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

193

2023.09.27

python print用法与作用
python print用法与作用

本专题整合了python print的用法、作用、函数功能相关内容,阅读专题下面的文章了解更多详细教程。

19

2026.02.03

typedef和define区别
typedef和define区别

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

119

2023.09.26

define的用法
define的用法

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

389

2023.10.11

while的用法
while的用法

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

107

2023.09.25

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

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

186

2023.11.23

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

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

134

2025.11.27

function是什么
function是什么

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

499

2023.08.04

Python异步编程与Asyncio高并发应用实践
Python异步编程与Asyncio高并发应用实践

本专题围绕 Python 异步编程模型展开,深入讲解 Asyncio 框架的核心原理与应用实践。内容包括事件循环机制、协程任务调度、异步 IO 处理以及并发任务管理策略。通过构建高并发网络请求与异步数据处理案例,帮助开发者掌握 Python 在高并发场景中的高效开发方法,并提升系统资源利用率与整体运行性能。

37

2026.03.12

热门下载

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

精品课程

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

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