C++ Void 与 Int 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7707604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Void vs Int Functions
提问by soniccool
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;
void 和 int 函数之间有什么区别?我什么时候使用哪个?仅打印消息或变量值时会使用 void 吗?像 cout << 值;
Or would it just be text?
或者它只是文本?
And is int used when you actually do calculations within it?
当您实际在其中进行计算时是否使用 int ?
回答by srikanta
void
is used when you are not required to return anything from the function to the caller of the function.
void
当您不需要从函数向函数调用者返回任何内容时使用。
for eg.
例如。
void no_return_fn()
{
cout<< "This function will not return anything" << endl;
return; // returns nothing or void
}
int
is used when you have to return an integer value from the function to the caller of the function
int
当你必须从函数返回一个整数值给函数的调用者时使用
for eg.
例如。
int return_sum_of_integers_fn(int a, int b)
{
cout<< "This function returns an integer" << endl;
return (a + b); // returns an integer
}
回答by Beta
Do you want the function to returnanything? If not, then it should be void
. If you want it to return an int
, then it should be int
. If you want it to return something else, then it should have some other return type.
你想让函数返回任何东西吗?如果没有,那么它应该是void
。如果您希望它返回一个int
,那么它应该是int
。如果你想让它返回别的东西,那么它应该有一些其他的返回类型。
回答by Ahmed Khalaf
Some prefer using functions that return int to indicate some errors or special cases. Consider this code:
有些人更喜欢使用返回 int 的函数来指示某些错误或特殊情况。考虑这个代码:
int print (int* ptr)
{
if (ptr == NULL)
{
return -1; // Error code.
}
std::cout << *ptr;
return 1; // Success code.
}
回答by Fitz Chen
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 时,这意味着您不希望该函数返回任何内容。而 int 返回可能是函数中的计算结果,或者指示返回时的状态,例如错误号或其他可以告诉您函数执行时发生了什么的东西。
回答by Kusavil
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.
就像你在上面听到的, void
不返回值,所以当你不需要这样做的时候你可以使用它。例如,您可以使用 'void' 不仅打印文本,而且主要用于修改参数(更改您已有的内容),因此您不需要返回值。
Let's say you want to find int c
, which is sum two (integer) numbers, a
and b
(so a+b=c). You can write a function which adds these numbers and assign it's value to c
假设您要查找 int c
,它是两个(整数)数字的总和, a
并且 b
(因此 a+b=c)。您可以编写一个函数,将这些数字相加并将其赋值给 c
int c;
int sum (int a, int b)
{
return a+b;
}
c = sum(a,b);
While using void
, you will just modificate c
, so instead of writing c = function(arguments)
, you will have function(c)
which modifies c
, for example:
在使用 时void
,您只需修改 c
,因此c = function(arguments)
您将拥有function(c)
which 修改c
,而不是编写 ,例如:
int c;
void sum(int a, int b, int c)
{
c = a+b;
}
sum(a,b,c);
After the last line, the 'c
' you have is equal the sum of 'a
' and 'b
' :-)
在最后一行之后,c
您拥有的 ' ' 等于 ' a
' 和 ' b
'的总和:-)
回答by Kusavil
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.
实际上,如果您正在为托管系统(例如 PC、Linux、Mac、Android)编译代码,这是必要的。然后 main 必须返回 int。如果您正在为独立系统(例如嵌入式微控制器)编译代码,或者您正在制作操作系统,那么 main 的返回类型可以是任何类型。
Though no error will occur if you use int or void but its not compliance according to standard C.
虽然如果您使用 int 或 void 不会发生错误,但它不符合标准 C。
回答by Juan Pablo
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
另一个区别是 void 函数不需要在其中返回:这 2 段代码是有效的并且不会生成编译器警告:Snippet 1
void msg1()
{
cout<< "This is a message." << endl;
return; // returns nothing or void
}
Snippet 2
片段 2
void msg2()
{
cout<< "This is a message." << endl;
}
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".
如果你声明一个函数为 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 :
由于没有返回,此代码很可能会显示编译器警告:
int test(){
printf("test");
}
回答by duffymo
You return void
to indicate that nothingis returned.
您返回void
表示没有返回任何内容。
An int return may or may not indicate that calculations have been performed (e.g. could just be a return code).
int 返回值可能会也可能不会表明计算已经执行(例如可能只是一个返回码)。