1

What is the difference between:

#include <stdio.h>

int f(int x){
    return x*x - 3;
}

int main(){
    int x = 4;
    printf("f(%d) = %d\n", x, f(x));
    return 0;
}

and

#include <stdio.h>

int f(int x){
    x*x - 3;
}

int main(){
    int x = 4;
    printf("f(%d) = %d\n", x, f(x));
    return 0;
}

Because the two compile without error (using gcc) and the two are working. Is the first return really important?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
naguam
  • 45
  • 8
  • 1
    If you want to return a value, yes, you must explicitly return it. It's an accident if it seems to work without it. – Steve Summit Jul 31 '18 at 15:24
  • 1
    `return` is not a _fonction_. – Sourav Ghosh Jul 31 '18 at 15:24
  • It is absolutely necessary. – Christian Gibbons Jul 31 '18 at 15:25
  • It depends where in the main it's necessary but if a function is declared as void you dont even Need a return but in every other function `int, double...` you Need the return value – user12346352 Jul 31 '18 at 15:26
  • 2
    MSVC gives **two** warnings: "'-': operator has no effect; expected operator with side-effect" and "'f': must return a value". So crank up your warning levels (or take them seriously if they were given). – Weather Vane Jul 31 '18 at 15:27
  • 3
    @SouravGhosh return could be a fonction. You don't know. – nicomp Jul 31 '18 at 15:27
  • 1
    @nicomp oh right, poor me (_goes and sulks in a corner..._) – Sourav Ghosh Jul 31 '18 at 15:28
  • If you want to see how this is abused horribly, look into code golf where you will see the "per compiler" rules for how it treats a functions without a return. If you do not use a return statement (outside of main) you are invoking undefined behaviour. – Michael Dorgan Jul 31 '18 at 15:28
  • @SouravGhosh actually, you cannot return a function type. You can return a pointer to a function, but not a function. So you were correct :) come back from the corner. – bolov Jul 31 '18 at 15:29
  • many c compilers dont warn about this on their default settings. It should really be an unconditional fatal – pm100 Jul 31 '18 at 15:29
  • @MichaelDorgan Even a void function needs a return statement? – nicomp Jul 31 '18 at 15:29
  • @nicomp nope, it does not. – Sourav Ghosh Jul 31 '18 at 15:30
  • @SouravGhosh I didn't think so, but above is written " If you do not use a return statement (outside of main) you are invoking undefined behaviour." – nicomp Jul 31 '18 at 15:32
  • 1
    @nicomp read it the other way, if you're trying to use the return value from a function which has a `void` return type, you're in trouble already. :) I guess that assumes a no-void returning function. – Sourav Ghosh Jul 31 '18 at 15:33
  • 1
    @nicomp [see this also](https://stackoverflow.com/q/9003283/2173917). – Sourav Ghosh Jul 31 '18 at 15:35
  • @SouravGhosh Makes sense. Now I will research "fonction" and write a scholarly paper on it. – nicomp Jul 31 '18 at 15:35
  • @user12346352 `main` is the one function of non-`void` type that need not `return` anything: in this case the compiler will add `return 0;` – Weather Vane Jul 31 '18 at 15:36
  • 2
    @nicomp [here's something to get you started with](https://fr.wikipedia.org/wiki/Fonction_(math%C3%A9matiques)). – Sourav Ghosh Jul 31 '18 at 15:36

1 Answers1

7

The second snippet invokes undefined behaviour.

A function, which reaches the ending } and the return value is used in the caller, invokes undefined behavior.

Quoting C11, chapter §6.9.1

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

So, yes, for a function

  • returning anything other than void (and whose value is going to be used in caller), must include a return statement with an expression, which has a type same as the return type of the function.
  • returning void, can have a return statement without any expression. However, this can be also be omitted without any trouble.

Note: Just because "your" compiler (settings) complies the code and produces a binary, does not mean the code is right. Use all the warning flags and strict checks, they will save you lots of trouble.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • I know if the compiler compile, it is not necessary right. But I had not explicit warning, that's why I asked the question. Thank you :) (last edit with clang there is a warning. I was using gcc and tcc (to test) with no warnings). I searched but I didn't really understand, know yes thank you. – naguam Jul 31 '18 at 15:47
  • - > naguam when in doubt, use the highest level of strict checking. As mentioned in the comment by @weathervane, you should have seen the warnings already. – Sourav Ghosh Jul 31 '18 at 15:49
  • @naguam: Since the behavior is *undefined*, the compiler is not required to issue any sort of diagnostic if you forget the `return` statement. Most will if you raise the warning level, though. – John Bode Jul 31 '18 at 16:03
  • doesn't the result of the last line get sent as the return value when no explicit return statement is mentioned? – Divakar Rajesh Jul 31 '18 at 16:45
  • 1
    @DivakarRajesh nope, not according to the C standard.. – Sourav Ghosh Jul 31 '18 at 16:59
  • (Coming from a recent dup:) I would emphasize that it is perfectly OK for non-void functions **in C** to return without a `return`. The responsibility is strictly with the caller to not use that non-value, and it is a run-time problem: When a branch has been executed which `return`ed a value the caller can use it, if not, then not. That even makes sense: A caller may know when a value is impossible to compute and call a function only for the side effects. – Peter - Reinstate Monica Mar 09 '20 at 15:05
  • 1
    @DivakarRajesh Some languages do that, e.g. Alogol68. The C comma operator has that semantic as well. – Peter - Reinstate Monica Mar 09 '20 at 15:18