如果void类型函数没有返回什么会发生什么

时间:2021-01-26 16:07:27

Let's say we have function like this

假设我们有这样的功能

void test() {return;}

Is is it correct C code? I just tested it in mingw and compiler says nothing, the same for

这是正确的C代码吗?我只是在mingw中测试它,编译器什么也没说,同样的

void test() {return 1;}

So I guess I have really outdated compiler.

所以我想我的编译器已经过时了。

What should happen in given cases in both C/C++?

在C / C ++的特定情况下会发生什么?

EDIT:

The return 1; gives me a warning. Does this mean return; is correct?

返回1;给我一个警告。这是否意味着回报;是正确的?

3 个解决方案

#1


17  

C++11(ISO/IEC 14882:2011) §6.6.3 The return statement

C ++ 11(ISO / IEC 14882:2011)§6.6.3返回语句

A return statement without an expression can be used only in functions that do not return a value, that is, a function with the return type void, a constructor, or a destructor. A return statement with an expression of non-void type can be used only in functions returning a value

不带表达式的return语句只能用于不返回值的函数,即返回类型为void的函数,构造函数或析构函数。表达式为非void类型的return语句只能在返回值的函数中使用

C11(ISO/IEC 9899:201x) §6.8.6.4 The return statement

C11(ISO / IEC 9899:201x)§6.8.6.4退货声明

A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.

带有表达式的return语句不应出现在返回类型为void的函数中。不带表达式的return语句只能出现在返回类型为void的函数中。

However, C89/C90 only constraints on half of it:

但是,C89 / C90只限制了它的一半:

C89/C90(ISO/IEC 9899:1990) §3.6.6.4 The return statement

C89 / C90(ISO / IEC 9899:1990)§3.6.6.4退货声明

A return statement with an expression shall not appear in a function whose return type is void .

带有表达式的return语句不应出现在返回类型为void的函数中。

In the C11 Forward section, it lists all the major changes in the third(i.e, C11) and second edition(i.e, C99). The last one of them is:

在C11前进部分,它列出了第三版(即C11)和第二版(即C99)的所有主要变化。最后一个是:

Major changes in the second edition included:

第二版的主要变化包括:

...

— return without expression not permitted in function that returns a value (and vice versa)

- 返回没有表达式的返回不允许返回值的函数(反之亦然)

This means that the constraint change of function return type is changed since C99.

这意味着自C99以来函数返回类型的约束更改已更改。

#2


0  

Void returns nothing so its obvious compiler won't do anything. If you want to return a value you must define the type whether int ,pointer node etc. etc.

Void没有返回任何内容,因此它明显的编译器不会做任何事如果要返回值,则必须定义int,指针节点等类型。

#3


0  

The return statement without an expression is the correct way to return from a function that returns void.

没有表达式的return语句是从返回void的函数返回的正确方法。

But, if you want to provide an expression in the return statement, then you should consider:

但是,如果要在return语句中提供表达式,则应考虑:

return (void)(expr);

Here, the expr is any expression that you want to include to have side-effects. As a suggestion, if you want to place a statement for its side-effect, then you should place it before the return statement as :

这里,expr是您想要包含的具有副作用的任何表达式。作为一个建议,如果你想为它的副作用放置一个声明,那么你应该把它放在return语句之前:

expr;
return;

#1


17  

C++11(ISO/IEC 14882:2011) §6.6.3 The return statement

C ++ 11(ISO / IEC 14882:2011)§6.6.3返回语句

A return statement without an expression can be used only in functions that do not return a value, that is, a function with the return type void, a constructor, or a destructor. A return statement with an expression of non-void type can be used only in functions returning a value

不带表达式的return语句只能用于不返回值的函数,即返回类型为void的函数,构造函数或析构函数。表达式为非void类型的return语句只能在返回值的函数中使用

C11(ISO/IEC 9899:201x) §6.8.6.4 The return statement

C11(ISO / IEC 9899:201x)§6.8.6.4退货声明

A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.

带有表达式的return语句不应出现在返回类型为void的函数中。不带表达式的return语句只能出现在返回类型为void的函数中。

However, C89/C90 only constraints on half of it:

但是,C89 / C90只限制了它的一半:

C89/C90(ISO/IEC 9899:1990) §3.6.6.4 The return statement

C89 / C90(ISO / IEC 9899:1990)§3.6.6.4退货声明

A return statement with an expression shall not appear in a function whose return type is void .

带有表达式的return语句不应出现在返回类型为void的函数中。

In the C11 Forward section, it lists all the major changes in the third(i.e, C11) and second edition(i.e, C99). The last one of them is:

在C11前进部分,它列出了第三版(即C11)和第二版(即C99)的所有主要变化。最后一个是:

Major changes in the second edition included:

第二版的主要变化包括:

...

— return without expression not permitted in function that returns a value (and vice versa)

- 返回没有表达式的返回不允许返回值的函数(反之亦然)

This means that the constraint change of function return type is changed since C99.

这意味着自C99以来函数返回类型的约束更改已更改。

#2


0  

Void returns nothing so its obvious compiler won't do anything. If you want to return a value you must define the type whether int ,pointer node etc. etc.

Void没有返回任何内容,因此它明显的编译器不会做任何事如果要返回值,则必须定义int,指针节点等类型。

#3


0  

The return statement without an expression is the correct way to return from a function that returns void.

没有表达式的return语句是从返回void的函数返回的正确方法。

But, if you want to provide an expression in the return statement, then you should consider:

但是,如果要在return语句中提供表达式,则应考虑:

return (void)(expr);

Here, the expr is any expression that you want to include to have side-effects. As a suggestion, if you want to place a statement for its side-effect, then you should place it before the return statement as :

这里,expr是您想要包含的具有副作用的任何表达式。作为一个建议,如果你想为它的副作用放置一个声明,那么你应该把它放在return语句之前:

expr;
return;