void 与 Int 函数
void 和 int 函数有什么区别?我什么时候使用哪个?仅打印消息或变量值时会使用 void 吗?就像 cout <<价值;
或者只是文本?
当你实际在其中进行计算时,是否使用了 int ?
What would be the different between void and int functions? When do i use which? Would void be used when just printing out a message or a variable value? like cout << value;
Or would it just be text?
And is int used when you actually do calculations within it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
当不需要从函数返回任何内容给函数的调用者时,可以使用
void
。例如。
当您必须从函数返回整数值给函数的调用者时,使用
int
,例如。
void
is used when you are not required to return anything from the function to the caller of the function.for eg.
int
is used when you have to return an integer value from the function to the caller of the functionfor eg.
您希望该函数返回任何内容吗?如果不是,那么它应该是
void
。如果您希望它返回int
,那么它应该是int
。如果您希望它返回其他内容,那么它应该具有其他返回类型。Do you want the function to return anything? If not, then it should be
void
. If you want it to return anint
, then it should beint
. If you want it to return something else, then it should have some other return type.有些人喜欢使用返回 int 的函数来指示某些错误或特殊情况。考虑这段代码:
Some prefer using functions that return int to indicate some errors or special cases. Consider this code:
当您使用 void 时,这意味着您不希望函数返回任何内容。
而 int 返回可能是函数中计算的结果,或者指示返回时的状态,例如错误号或其他可以告诉您函数执行时发生了什么的信息。
when you use void, it means you don't want anything returned from the function.
while an int return may be a result of calculation in the function, or indicate the status when it returning, such as an error number or something else that can tell you what has happened when the function executing.
正如您在上面听到的,
void
不返回值,因此您可以在不需要时使用它。例如,您不仅可以使用“void”来打印文本,还可以主要用于修改参数(更改已有的内容),因此不需要返回值。
假设您要查找
int c
,它是两个(整数)数字a
和b
的和(因此 a+b=c) 。您可以编写一个函数,将这些数字相加并将其值赋给c
在使用
void
时,您只需修改c
,因此无需编写c = function(arguments)
,您将拥有function(c)
来修改c
,例如:在最后一行之后,'< code>c' 你的总和等于“
a
”和“b
”:-)Like you heard above,
void
doesn't return value, so you use it when you don't need to do this.For example, you can use 'void' not only to print text, but mainly to modificate a parameters (change something you already have), so you don't need to return a value.
Let's say you want to find
int c
, which is sum two (integer) numbers,a
andb
(so a+b=c). You can write a function which adds these numbers and assign it's value toc
While using
void
, you will just modificatec
, so instead of writingc = function(arguments)
, you will havefunction(c)
which modifiesc
, for example:After the last line, the '
c
' you have is equal the sum of 'a
' and 'b
' :-)您返回
void
表示没有返回任何内容。int 返回可能表示也可能不表示计算已执行(例如可能只是返回代码)。
You return
void
to indicate that nothing is returned.An int return may or may not indicate that calculations have been performed (e.g. could just be a return code).
另一个区别是 void 函数不需要在其中有 return:
这 2 段代码是有效的,不会生成编译器警告:
代码段 1
代码段 2
两者具有相同的行为,并且不显示警告。
如果您将函数声明为非 void 并且不返回值,则编译器可能会显示警告,例如“警告:返回非 void 的函数中没有 return 语句”。
是否显示此错误取决于发送到编译器的修饰符参数。
由于没有返回,此代码可能会显示编译器警告:
Another difference is that void function do not require to have a return inside it:
This 2 snippets of code are valid and do not generate a compiler warning:
Snippet 1
Snippet 2
Both have the same behavior and no warning is displayed.
If you declare a function non void and you do not return a value your compiler is likely to display a WARNING like " warning: no return statement in function returning non-void".
It's depends on modifier parameters sent to compiler if this error is displayed or not.
This code is likey to display a compiler warning since no return :
实际上,如果您正在为托管系统(例如 PC、Linux、Mac、Android)编译代码,则这是必要的。那么main必须返回int。如果您正在为独立系统(例如嵌入式微控制器)编译代码,或者如果您正在制作操作系统,则 main 的返回类型可以是任何类型。
虽然使用 int 或 void 不会发生错误,但它不符合标准 C。
Actually,It is necessary if you are compiling your code for a hosted system, such as PC, Linux, Mac, Android. Then main must return int. If you are compiling code for a freestanding system, such as embedded microcontrollers, or if you are making an OS, then the return type of main can be anything.
Though no error will occur if you use int or void but its not compliance according to standard C.