简体   繁体   English

Void与Int函数

[英]Void vs Int Functions

What would be the different between void and int functions? void和int函数之间有什么不同? When do i use which? 我什么时候使用哪个? Would void be used when just printing out a message or a variable value? 只打印出消息或变量值时会无效吗? like cout << value; 像cout << value;

Or would it just be text? 或者只是文字?

And is int used when you actually do calculations within it? 当你在其中实际进行计算时,是否使用了int?

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
}

Do you want the function to return anything? 你想让函数返回任何东西吗? If not, then it should be void . 如果没有,那么它应该是void If you want it to return an int , then it should be int . 如果你想让它返回一个int ,那么它应该是int If you want it to return something else, then it should have some other return type. 如果你想让它返回别的东西,那么它应该有一些其他的返回类型。

Some prefer using functions that return int to indicate some errors or special cases. 有些人更喜欢使用返回int的函数来指示一些错误或特殊情况。 Consider this code: 考虑以下代码:

int print (int* ptr)
{
    if (ptr == NULL)
    {
        return -1; // Error code.
    }

    std::cout << *ptr;

    return 1; // Success code.
}

when you use void, it means you don't want anything returned from the function. 当你使用void时,这意味着你不希望从函数返回任何东西。 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. 而int返回可能是函数计算的结果,或者指示返回时的状态,例如错误号或其他可以告诉您函数执行时发生了什么的事情。

Like you heard above, void doesn't return value, so you use it when you don't need to do this. 就像你上面听到的那样, void不会返回值,所以当你不需要这样做时你就可以使用它。 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'来打印文本,但主要用于修改参数(更改您已有的内容),因此您无需返回值。

Let's say you want to find int c , which is sum two (integer) numbers, a and b (so a+b=c). 假设你要找到int c ,它是二(整数)数, ab (所以a + b = c)。 You can write a function which adds these numbers and assign it's value to 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) ,而是使用修改c function(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 '的总和:-)

Actually,It is necessary if you are compiling your code for a hosted system, such as PC, Linux, Mac, Android. 实际上,如果您正在为托管系统编译代码,例如PC,Linux,Mac,Android,则必须这样做。 Then main must return int. 然后main必须返回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. 如果您正在为独立系统编译代码,例如嵌入式微控制器,或者如果您正在制作操作系统,那么main的返回类型可以是任何东西。

Though no error will occur if you use int or void but its not compliance according to standard C. 虽然如果使用int或void但不符合标准C,则不会出现错误。

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". 如果声明一个非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");
}

You return void to indicate that nothing is returned. 返回void表示没有返回任何内容。

An int return may or may not indicate that calculations have been performed (eg could just be a return code). int返回可能表示也可能不表示已执行计算(例如,可能只是返回码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM