0

I am rather in doubts about getting some problems while coding with a few recursive functions. Now here I am giving a simple code with nested function call,so that I can point to my exact problem.

int main(void) 
{

    int i;
    i=a();
    printf("%d\n",i);
    return 0;
}
int a()
{
  return b();

}
int b()
{
    return 9;
}

well,no problem at all,it gives output as 9. But if i redefine function a() as:

int a()
{
b();
int new=0; //not significant
}

Again it producing valid output, .i.e,

9

here, though I removed return keyword I was not getting any compilation error,neither the value of i in the output was wrong...(I expected garbage or something like that). How these things are handled?

RahulCoffee
  • 56
  • 11
  • It is not an duplicate question. My problem is somewhat different from other return type queries and problems. – RahulCoffee May 16 '16 at 02:53
  • 1
    @RCoffee: Sorry, but the chosen duplicate accurately reflects your modified code — it is a good duplicate. The behaviour is undefined; any result is OK and there is no way to explain what result you get other than shrug your shoulders and say "oh well". – Jonathan Leffler May 16 '16 at 04:32
  • I am accepting your ans @CodingBatman as because it is highly logical what you pointed. But fair to say I have tried in gcc(I use Fedora) and dev c++ also. Both of them showing the output what I am returning from function b(). – RahulCoffee May 16 '16 at 05:26
  • @JonathanLeffler I think it's really time for me to shrug my shoulders saying "oh well". :) Actually I was trying recursive Binary search and Towerofhanoi Algo. And in the coding I mistakenly skipped return keyword before recursive calls. But I get no compilation error and get correct output also. – RahulCoffee May 16 '16 at 05:31
  • @RCoffee, but you do get a compiler warning. Please check the compilation tab for the [warning](http://cpp.sh/4b2ic). You need to use the `-Wall` compiler flag during compilation. – abhishek_naik May 16 '16 at 05:36
  • You can't be using fussy enough compilation warnings. I took the 5 lines of code for the second version of `a()` in `bare-minimum.c` and compiled it with my default options (`gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror -c bare-minimum.c`) and got 'errors' (warnings converted to errors) aplenty: _[…continued…]_ – Jonathan Leffler May 16 '16 at 05:41
  • _[…continuation…]_ `bare-minimum.c:1:5: error: function declaration isn’t a prototype [-Werror=strict-prototypes]` `bare-minimum.c: In function ‘a’:` `bare-minimum.c:1:5: error: old-style function definition [-Werror=old-style-definition]` `bare-minimum.c:3:5: error: implicit declaration of function ‘b’ [-Werror=implicit-function-declaration]` `bare-minimum.c:4:9: error: unused variable ‘new’ [-Werror=unused-variable]` `bare-minimum.c:5:1: error: control reaches end of non-void function [-Werror=return-type]` `cc1: all warnings being treated as errors` – Jonathan Leffler May 16 '16 at 05:41
  • Hmm! I have tried -Wall now(I didn't know Wall before) , it's giving me some warnings as you have mentioned. But no errors. – RahulCoffee May 16 '16 at 06:05
  • @JonathanLeffler please check the output of this code...whether 2 or not.. int f(int n) { if(n==2) return n; else f(n-1); } int main() { printf("%d",f(6)); return 0; } – RahulCoffee May 16 '16 at 06:14
  • I used `-Werror` to convert all warnings into errors; it's a good discipline. This is what I use for all my code — when I'm not using still more stringent options (so this is the base level; I don't normally run the code until it compiles cleanly with these options). – Jonathan Leffler May 16 '16 at 06:24
  • Ok I will also try to use it. -Werror. Thanks@JonathanLeffler – RahulCoffee May 16 '16 at 06:34
  • When I compiled your sample code without my usual set of compilation options (it fails to compile horribly with them), then I got the output 2 from GCC 6.1.0 on Mac OS X 10.11.4. But it doesn't mean anything; the code is malformed and any result is meaningless. It looks like the `return n;` sets %eax and it isn't altered as the recursion unwinds, so it stays as 2, but that depends on the quirks of the implementation and there's no guarantee that you'll get back 2. For example, using `else printf("= %d\n", f(n-1));` prints `= 2`, then `= 4` multiple times, and 4 as the result. – Jonathan Leffler May 16 '16 at 06:39
  • ok. Have learnt it. May I have your mail id so that I can contact you if I get this kind of problems?? And would you please discuss %eax you mentioned... – RahulCoffee May 16 '16 at 07:11

1 Answers1

0

In your second piece of code:

int a()
{
    b();
    int new=0; //not significant
}

The return type of a() is int. However, in your code, you are not returning anything. This typically results in undefined behavior. So, you may get any value (you said you are getting 9, while using gcc on ideone, I am getting 0). It is undefined behavior, so no-one can say exactly what you'll get.

It will depend on the compiler, as pointed out in the SO Answer, but the compiler isn't obliged to document what happens, nor is it required to be consistent in the returned value. Different calls may return different values.

Community
  • 1
  • 1
abhishek_naik
  • 1,287
  • 2
  • 15
  • 27