开发者

C warning 'return' with no value, in function returning non-void

开发者 https://www.devze.com 2022-12-22 09:13 出处:网络
I have this warning. warning : \'return\' with no value, in functi开发者_如何学编程on returning non-void.

I have this warning.

warning : 'return' with no value, in functi开发者_如何学编程on returning non-void.


You have something like:

int function(void)
{
    return;
}

Add a return value, or change the return type to void.

The error message is very clear:

warning : 'return' with no value, in function returning non-void.

A return with no value is similar to what I showed. The message also tells you that if the function returns 'void', it would not give the warning. But because the function is supposed to return a value but your 'return' statement didn't, you have a problem.

This is often indicative of ancient code. In the days before the C89 standard, compilers did not necessarily support 'void'. The accepted style was then:

function_not_returning_an_explicit_value(i)
char *i;
{
    ...
    if (...something...)
        return;
}

Technically, the function returns an int, but no value was expected. This style of code elicits the warning you got - and C99 officially outlaws it, but compilers continue to accept it for reasons of backwards compatibility.


This warning also happens if you forget to add a return statement as the last statement:

int func(){}

If you don't specify the return type of a function it defaults to int not to void so these are also errors:

func(){}
func(){ return; }

If you really do not need to return a value you should declare your function as returning void:

void func(){}
void func(){ return; }


This warning happens when you do this:

int t() { return; }

Because t() is declared to return an int, but the return statement isn't returning an int. The correct version is:

int t() { return 0; }

Obviously your code is more complicated, but it should be fairly easy to spot a bare return in your code.


while making a new class declare a class as void i.g.

void sum(int a, int b)
{
 //function
}


Whenever you are not declaring any function with void(null) return value,

void examPle(){//Function block with no return type }

, you should always specify a valid return type you expext as the o/p. Below is the basic syntax of what you should be returning when writing your main block.

int main(){ 
return 0;
}

here return 0; means success.

0

精彩评论

暂无评论...
验证码 换一张
取 消