Is it mandatory to use return and void in the functions in C?
According to the C Standard (6.7.6.3 Function declarators (including prototypes)
- The special case of an unnamed parameter of type void as the only
item in the list specifies that the function has no parameters.
- An identifier list declares only the identifiers of the parameters
of the function. An empty list in a function declarator that is part
of a definition of that function specifies that the function has no
parameters. The empty list in a function declarator that is not part
of a definition of that function specifies that no information about
the number or types of the parameters is supplied.145)
Footnote 145 refers to "Future directions §6.11.6" and "The use of function declarators with empty parentheses (not prototype-format parameter
type declarators) is an obsolescent feature."
Thus declaration
void f( void );
means that the function has no parameters.
Declaration
void f();
means that the number and types of parameters are unknown.
Declaration that at the same time is a definition
void f()
{
}
means that the function does not have parameters.
As for the return statement in a function that has return type void
then if it is the last statement of the function then usually it is omitted. Using the last return statement can only confuse the reader of the code because he has to be sure that the absence of an expression in the return statement is not a typo.
What does a non void C function without return statement actually return?
- what does a non void function without any return statement actually return ? Could it be the last allocated variable on the stack ?
The C standard does not define the behavior if a function does not execute return
with a value and the return value of the function is used.
(There is some evidence that, in some situations, GCC deliberately returns the value of the last full expression evaluated in the function, but that was discussed in comments some time ago, and I do not have a reference.)
GCC raises a warning for that , but not an error. Wouldn't a compiler error be actually more pertinent for such a case instead of a compiler warning ?
The C standard does not prohibit a function with a non-void return type from returning without returning a value (by allowing program control to flow to the terminating }
of the function definition). So a conforming compiler should allow this in the function definition, meaning it can warn but should not produce an error. It is allowed to call such a function and not use its return value; that has behavior defined by the C standard.
If the compiler can see that a function call uses the return value of a function that does not return a value, the compiler could produce an error for that.
It is occasionally useful for a function with a non-void return type to return a value in some situations and not in others, such as a function that accepts a command to perform and returns a value when a “Get value of setting” command is performed and does not return a value when a “Set value of setting” command is performed.
return statement in void function in C
Of course you can use return;
in a void
function. How else would you return early from such a function?
In your case the statement is redundant as it's reached on all control paths, but a compiler will certainly not issue a diagnostic in that instance.
(Note that you must return
explicitly from a non-void
function, with the exception of main()
, where a compiler must generate return 0;
if missing.
Is a return statement always necessary in a c function?
A return statement is not necessary.
If your function has a non-void return type, and you don't return a value, and the caller uses the return value, then it causes undefined behaviour.
The compiler gives a warning because your function has a non-void return type, and it thinks you might have meant to return a value but forgotten.
The code you posted doesn't correspond to the compiler message, unless PUBLIC
is defined to something strange.
Is a blank return statement at the end of a function whos return type is void necessary?
No; that is not needed.
You only need to write return;
if you want to return early and skip the rest of the function.
Is it valid to omit the return statement of a non-void function template that throw
are example 1 and example 2 valid?
Yes.
Or we have UB/ill-formed.
No.
Is it valid to omit the return statement of a non-void function/function template that throw
Yes. A non-void returning function must either throw or return avalue. It cannot do both at the same time.
So is
int func(){ int x = 4; throw; return x;}
valid
It's valid. But it never returns a value. Everything after the throw is dead code.
Related Topics
What Does '<Cuchar>' Provide, and Where Is It Documented
What's the Purpose of Using Braces (I.E. {}) for a Single-Line If or Loop
What Is the Simplest Way to Convert Array to Vector
C++ Correct Way to Return Pointer to Array from Function
Compile Time Sizeof_Array Without Using a MACro
How to Pre-Allocate Memory for a Std::String Object
Can a Std::String Contain Embedded Nulls
Conversion from String Literal to Char* Is Deprecated
Detect If Program Is Running with Full Administrator Rights
Benchmarking (Python VS. C++ Using Blas) and (Numpy)
Are Parentheses Around the Result Significant in a Return Statement
Visual Studio 2015 Doesn't Have Cl.Exe