为什么将 void 与函数一起使用?
Why use void with a function?
我知道 void return 没有值。
那么它是如何结合函数起作用的呢?
我的理解是函数的目的是return在用它做一些事情之后得到一条信息。
所以我为什么要 return 没有价值,这将如何受益?
My understanding is that the purpose of a function is to return a
piece of information after doing something with it.
在某些(大多数)编程语言中,函数也有副作用。某些功能的用途仅限于副作用, return 值不是必需的。此类函数具有 void
return 类型。
一些副作用示例可能是:
- 更新全局
- 用户不想知道操作状态的文件操作、日志记录等
- 释放资源
C++ Programming Language Stroustrup 第 4 版书籍
When declaring a function, you must specify the type of the value returned. Logically, you would expect to be able to indicate that a function didn’t return a value by omitting the return type. However, that would make a mess of the grammar (§iso.A). Consequently, void is used as a ‘‘pseudo return type’’ to indicate that a function doesn’t return a value.
编辑:
当您不希望调用函数 return 中的内容时,我们使用 void
函数。
If void() does not return a value, why do we use it?
我知道 void return 没有值。 那么它是如何结合函数起作用的呢?
我的理解是函数的目的是return在用它做一些事情之后得到一条信息。
所以我为什么要 return 没有价值,这将如何受益?
My understanding is that the purpose of a function is to return a piece of information after doing something with it.
在某些(大多数)编程语言中,函数也有副作用。某些功能的用途仅限于副作用, return 值不是必需的。此类函数具有 void
return 类型。
一些副作用示例可能是:
- 更新全局
- 用户不想知道操作状态的文件操作、日志记录等
- 释放资源
C++ Programming Language Stroustrup 第 4 版书籍
When declaring a function, you must specify the type of the value returned. Logically, you would expect to be able to indicate that a function didn’t return a value by omitting the return type. However, that would make a mess of the grammar (§iso.A). Consequently, void is used as a ‘‘pseudo return type’’ to indicate that a function doesn’t return a value.
编辑:
当您不希望调用函数 return 中的内容时,我们使用 void
函数。
If void() does not return a value, why do we use it?