1

Let's take this piece of code:

node_t* create_tree_node(void* data){
    node_t* node = (node_t*) malloc(sizeof(node_t)); 
    node->data = data ; 
    node->left = NULL ; 
    node->right = NULL ; 
}

Now, If you noticed, there's no return statement, let's say we forgot to put one.

I actually tried a similar code (imagine node_t being the habitual structure for a node in a binary tree, ie, a field for the data, and pointers to the next left and right nodes), and, when I did this:

node_t* node = create_tree_node((void*) some_random_value) ; 

I was able to create a node with the desired value , and save it in a binary tree, use it, and free it.

So two question:

  1. what does a non void function without any return statement actually return ? Could it be the last allocated variable on the stack ?
  2. 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?
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Amine Bensalem
  • 362
  • 3
  • 15
  • 8
    Using values returned from function that didn't use `return` statement to return invokes *undefined behavior*. – MikeCAT Aug 03 '20 at 16:26
  • Does this answer your question? [Implicit int return value of C function](https://stackoverflow.com/questions/10079089/implicit-int-return-value-of-c-function) – Joshua Yonathan Aug 03 '20 at 16:27
  • 2
    For your second question, pass `-Werror` to your compiler and all of your warnings become errors! – Kevin Aug 03 '20 at 16:28
  • @JoshuaYonathan wrong - it is something else. Implicit return type and parameters is an obsolete feature `foo() {return 1;}` – 0___________ Aug 03 '20 at 16:40
  • 1
    The `void` type was not originally part of C; any function that wasn't explicitly declared to return a particular type was implicitly typed to return `int`. Since it was common to write `int` functions that were only executed for their side effects (and still is, I doubt many people check the return value of `printf`), the language does not *require* an explicit `return` statement from non-`void` functions. The only requirement is that if a `return` statement *is* present in a non-`void` function, then it *must* be followed by some expression. – John Bode Aug 03 '20 at 16:57

1 Answers1

4
  1. 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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312