2-3 Void 函数(不返回值的函数)
在之前的课程(2.1——函数介绍)中,我们指出函数定义的语法如下所示:
returnType identifier() // identifier replaced with the name of your function
{
// Your code here
}
虽然我们展示了返回类型为 void 的函数示例,但并未探讨其含义。在本节课中,我们将深入探讨返回类型为 void 的函数。
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 returned
return 0;
}

在上例中,printHi函数具有实用行为(它会打印“Hi”),但无需向调用方返回任何值。因此,printHi被赋予void返回类型。
当 main 调用 printHi 时,printHi 中的代码执行完毕后打印出“Hi”。printHi 结束时,控制权返回 main 并继续执行程序。
不返回值的函数称为非值返回函数non-value returning function(或 void 函数void function)。
void 函数无需返回语句
void 函数在函数结束时会自动返回给调用方,无需显式返回语句。
在 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 the return will happen at the end of the function anyway!
} // function will return to caller here
int main()
{
printHi();
return 0;
}

最佳实践
不要在不返回值的函数末尾放置返回语句。
空函数不能用于需要返回值的表达式
某些类型的表达式需要返回值。例如:
#include <iostream>
int main()
{
std::cout << 5; // ok: 5 is a literal value that we're sending to the console to be printed
std::cout << ; // compile error: no value provided
return 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 returned
std::cout << printHi(); // compile error
return 0;
}

首次调用 printHi() 时,其运行环境并不需要返回值。由于该函数本身不返回值,因此这种情况是可行的。
而第二次对函数 printHi() 的调用甚至无法编译通过。函数 printHi 的返回类型为 void,表示其不返回值。然而该语句试图将 printHi 的返回值发送给 std::cout 进行打印。std::cout 无法处理此操作(它该输出什么值?)。因此编译器会将此标记为错误。你需要注释掉这行代码才能使程序通过编译。
提示
某些语句需要提供值,而另一些则不需要。
当语句仅包含函数调用时(例如上例中的第一个 printHi()),我们调用函数是为了其行为而非返回值。这种情况下,我们可以调用不返回值的函数,也可以调用返回值的函数并忽略其返回值。
当函数调用发生在需要值的上下文中(如std::cout),则必须提供值。此类场景下只能调用返回值的函数。
#include <iostream> // Function that does not return a value void returnNothing() { } // Function that returns a value int returnFive() { return 5; } int main() { // When calling a function by itself, no value is required returnNothing(); // ok: we can call a function that does not return a value returnFive(); // ok: we can call a function that returns a value, and ignore that return value // When calling a function in a context that requires a value (like std::cout) std::cout << returnFive(); // ok: we can call a function that returns a value, and the value will be used std::cout << returnNothing(); // compile error: we can't call a function that returns void in this context return 0; }
从 void 函数中返回值会导致编译错误
尝试从不返回值的函数中返回值将导致编译错误:
void printHi() // This function is non-value returning
{
std::cout << "In printHi()" << '\n';
return 5; // compile error: we're trying to return a value
}
测验时间
问题 #1
检查以下程序,并说明它们的输出结果,或指出它们是否无法编译。
1a)
#include <iostream>
void printA()
{
std::cout << "A\n";
}
void printB()
{
std::cout << "B\n";
}
int main()
{
printA();
printB();
return 0;
}
显示答案
该程序将字母A和B分别打印在两行上。
1b)
#include <iostream>
void printA()
{
std::cout << "A\n";
}
int main()
{
std::cout << printA() << '\n';
return 0;
}
显示答案
该程序无法编译。函数 printA() 返回 void 类型,无法将其发送至 std::cout 进行打印。这将导致编译错误。

浙公网安备 33010602011771号