Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C void function with return value

Tags:

c

As far as I know, return statement in void function will throw an error.

But in the below program that is not the case.

Here the displayed output is 1. How comes?

main()
{
    int i=5;
    printf("%d",fun(fun(fun(i))));
}

void fun(int i)
{
    if (i%2)
    {
        return (i+(7*4)-(5/2)+(2*2));
    }
    else
    {
        return (i+(17/5)-(34/15)+(5/2));
    }
}
like image 645
venugopal Avatar asked Jul 25 '14 14:07

venugopal


People also ask

Can I use return in void function?

Void functions do not have a return type, but they can do return values.

What happens when a return statement is executed in a void function?

When a return statement contains an expression in functions that have a void return type, the compiler generates a warning, and the expression isn't evaluated.


1 Answers

A return statement with no expression:

void func(void) {
    return;
}

is perfectly legal in a void function. The legality of a return statement with an expression depends on the version of the C language you're using.

The 1990 C standard says:

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

The 1999 and 2011 editions of the standard both say:

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.

This is a constraint, meaning that a compiler must issue a diagnostic (possibly a non-fatal warning) for any program that violates it.

C90 permitted a return statement with no expression in a non-void function for historical reasons. Pre-ANSI C did not have the void keyword, so there was no way to define a function that didn't return a value. Programmers would omit the return type (which would default to int) and simply ignore it. The C90 rule allowed such old code to compile without error. You could still fail to return a value from a non-void function; the program's behavior is undefined if a caller attempts to use the (nonexistent) result. The 1999 standard tightened up the rules a bit.

Another problem with your program is that you call fun before its declaration is visible. Under C99 and later rules, this is illegal (though a compiler might merely warn about it). Under C90 rules, this is legal, but the compiler will assume that the function returns int. Your program's behavior is undefined, but your void function fun might happen to behave as if it returned a value, and a call to it might happen to behave as if it used that value.

C compilers tend to be fairly lax about some errors, so that old code (sometimes written before the first actual standard was published) will not be rejected. But your compiler should have at least warned you about the return statement, and probably about the invalid call. You should pay close attention to compiler warnings; they should be treated almost the same way as fatal errors. And you should use options to increase the number of things your compiler warns you about. If you're using gcc, use -std=c90, -std=c99, or -std=c11, along with -pedantic to enforce standard conformance. You can add -Wall-Wextra` to enable more warnings.

like image 169
Keith Thompson Avatar answered Sep 29 '22 13:09

Keith Thompson