I have a doubt on function returning a value. Consider the following program snippet.

int add(int a,int b) {
   int c=a+b;
   return;
   }
 
 int main()  {
   int a=10,b=20;
   printf("%d %d %d",a,b,add(a,b));
   return(0);
   }

The above program gives the output as
10 20 30

But the function is not returning any value. According to the K&R C, Section 4.1,

".......there need be no expression after return; in that case, no value is returned to the caller."

I will be grateful if anyone explains the logic behind this cryptic.

Thanks in advance.

Dani AI

Generated

Short answer: the program relies on undefined behavior. The C language requires a non-void function to provide a return value; if control reaches the end of such a function (or a bare return; is used) and the caller uses the result, the behavior is undefined (return statement, ).

Why it printed 30 anyway: on many ABIs an integer result is delivered in a register (on x86/x86-64 this convention uses the low return register). With the particular compiler, optimization level and platform you used, the computed sum happened to be left where the caller reads the return value — so it looked correct. That is incidental and not guaranteed across compilers, platforms, or optimization settings. was essentially right to point at the calling/return mechanism, and was right to point out that using strict/standards mode forces the compiler to diagnose the problem.

Practical fixes and checks:

  • Always return an actual value from functions declared to return a non-void type, or change the function to void if no value is intended.
  • Compile with a modern standard and warnings enabled to catch this class of errors early. Example flags to use with GCC/Clang:
    gcc -std=c11 -Wall -Wextra -Wpedantic -Wreturn-type -Werror -o prog prog.c
  • Use static analysis tools (clang-tidy, cppcheck) and enable sanitizers while debugging.

Treat this as a bug: relying on the observed output is brittle and non-portable. The correct fix is to return the computed value explicitly so the program's behavior is well-defined.

Recommended Answers

All 4 Replies

Your code probably shouldn't compile. add(int, int) is specified to return an int, but doesn't, which should raise a compile time error.

Compile your code in strict mode. I guess it should be error. Anyways return value in C on x86 is generally eax , where the result of the last calculation often happens to be placed. Maybe that's why you are seeing this result.

Your code probably shouldn't compile. add(int, int) is specified to return an int, but doesn't, which should raise a compile time error.

You are right. When I compiled in strict mode, it thrown a compilation error. But in normal mode it doesn't and it will act exactly as Grunt's explanation.
Anyway, thank you very much for your quick reply.

Compile your code in strict mode. I guess it should be error. Anyways return value in C on x86 is generally eax , where the result of the last calculation often happens to be placed. Maybe that's why you are seeing this result.

Cool explanation and I am cleared with all my doubts and even it has thrown an error when I compiled in strict mode as you expected.
Much appreciated reply:cool:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.