当前位置: 首页 > news >正文

C++中编写没有参数和返回值的函数

C++中编写没有参数和返回值的函数

返回值为 void

函数不需要将值返回给调用者。为了告诉编译器函数不返回值,返回类型为 void。例如:

#include <iostream>// void means the function does not return a value to the caller
void printHi()
{std::cout << "Hi" << '\n';// This function does not return a value so no return statement is needed
}int main()
{printHi(); // okay: function printHi() is called, no value is returnedreturn 0;
}

在上面的例子中,printHi 函数有一个有用的行为(它打印“Hi”),但它不需要返回任何东西给调用者。因此, printHi 被赋予了一个 void 返回类型。

当 main 调用 printHi 时,执行 printHi 中的代码,并打印“Hi”。在 printHi 结束时,控制返回到 main 并且程序继续。

不返回值的函数称为无返回值函数(或 void 函数)。

void 函数不需要 return 语句

void 函数将在函数结束时自动返回给调用者。不需要 return 语句。

可以在 void 函数中使用 return 语句(没有返回值)——这样的语句将导致函数在执行return声明时返回给调用者。无论如何,这与函数结束时发生的情况相同。因此,在void函数的末尾放一个空的return语句是多余的。

#include <iostream>// void means the function does not return a value to the caller
void printHi()
{std::cout << "Hi" << '\n';return; // tell compiler to return to the caller -- this is redundant since this will happen anyway!
} // function will return to caller hereint main()
{printHi();return 0;
}

void 函数不能用于需要计算值的表达式

某些类型的表达式需要值。例如:

#include <iostream>int main()
{std::cout << 5; // ok: 5 is a literal value that we're sending to the console to be printedstd::cout << ;  // compile error: no value providedreturn 0;
}

在上述程序中,需要在 std::cout << 的右侧提供要打印的值。如果没有提供值,编译器将产生语法错误。由于对 std::cout 的第二次调用没有提供要打印的值,这会导致错误。

现在考虑以下程序:

#include <iostream>// void means the function does not return a value to the caller
void printHi()
{std::cout << "Hi" << '\n';
}int main()
{printHi(); // okay: function printHi() is called, no value is returnedstd::cout << printHi(); // compile errorreturn 0;
}

第一次调用 printHi() 是在不需要值的上下文中调用的。由于函数不返回值,这很好。

对函数 printHi() 的第二个函数调用甚至无法编译。函数 printHi 有一个 void 返回类型,这意味着它不返回值。但是,此语句试图将 printHi 的返回值发送到 std::cout 以进行打印。std::cout 不知道如何处理(它会输出什么值?)。因此,编译器会将其标记为错误。您需要注释掉这行代码才能使您的代码编译。

例子

如果将显示 Hello World 的工作委托给一个函数,且该函数不做别的,则它就不需要任何参数(因为它除了显示 Hello World 外,什么也不做),也无需返回任何值(因为您不指望这样的函数能提供在其他地方有用的东西)。如下示例程序演示了一个这样的函数:

#include <iostream>
using namespace std;void SayHello();int main()
{SayHello();return 0;
}void SayHello()
{cout << "Hello World" << endl;return; // an empty return
}

输出:

Hello World

注意到第 3 行的函数原型将函数 SayHello()的返回类型声明为 void,即不返回任何值。因此,在第 11~14 行的函数定义中, 没有 return 语句。有些程序员喜欢在这种函数末尾包含一条空的 return 语句:

void SayHello()
{cout << "Hello World" << endl;return; // an empty return
}

该文章会更新,欢迎大家批评指正。

推荐一个零声学院的C++服务器开发课程,个人觉得老师讲得不错,
分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,
fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,
TCP/IP,协程,DPDK等技术内容
点击立即学习:C/C++后台高级服务器课程

http://www.lryc.cn/news/174189.html

相关文章:

  • SWC 流程
  • 怒刷LeetCode的第10天(Java版)
  • java框架-Springboot3-场景整合
  • 在Bat To Exe Converter,修改为当异常结束或终止时,程序重新启动执行
  • PythonWeb服务器(HTTP协议)
  • Northstar 量化平台
  • c语言进阶部分详解(经典回调函数qsort()详解及模拟实现)
  • win下 lvgl模拟器codeblocks配置
  • Quartus出租车计价器VHDL计费器
  • 浅谈单元测试:测试和自动化中的利用
  • 深度详解Java序列化
  • Linux下的网络编程——B/S模型HTTP(四)
  • Go语言入门篇
  • 基于springboot+vue的青年公寓服务平台
  • Spring-ImportSelector接口功能介绍
  • YOLOv5如何训练自己的数据集
  • 李航老师《统计学习方法》第1章阅读笔记
  • 基于微信小程序的背单词学习激励系统设计与实现(源码+lw+部署文档+讲解等)
  • VScode调试复杂C/C++项目
  • 【Hash表】字母异位词分组-力扣 49 题
  • 展示日志log4.properties
  • 基于PLE结合卡尔曼滤波的RSSI定位算法matlab仿真
  • uniapp项目实践总结(十九)版本更新和热更新实现方法
  • 一起学数据结构(8)——二叉树中堆的代码实现
  • Linux环境变量配置说明(配置jdk为例-摘录自尚硅谷技术文档)
  • idea常用插件笔记
  • 搜索二叉树【C++】
  • 华为云云耀云服务器L实例评测|认识redis未授权访问漏洞 漏洞的部分复现 设置连接密码 redis其他命令学习
  • 快速安装NGINX
  • 一台电脑远程内网的另外一台电脑,禁止远程的电脑连接外网,只允许内网连接