curious result

hooshdar3 (143)
Hi.Why is it that the following code doesn't print 5 consecutive zero in f1 calls?
1
2
3
4
5
6
7
8
9
10
11
12
13
f1(const int x)
{
         printf("%d", x);
         if(x==0)return 0;
         else {printf("\n%d", 2*f1(x-1) );//return 2*f1(x-1)+x*x;
          }
}
main()
{
      printf("\n*********main***********\n%d", f1(5));
      
      while(1);
}

Cubbi (1584)
What does it print?

It can't be compiled in C++ or modern C (missing return type), and it can't be compiled in the ancient C89, where this f1 returns int, because "//" was not a valid comment.

In any case, falling off the end of a value-returning function without a return statement has always been undefined behavior.
flony (4)
not in c, c + + but, first declaring data type returned by the function "f1".
missing the "# include <xxxxx>" "using namespace std;" the loop "while" is misspelled ...
should do the code again
hooshdar3 (143)
the orginal problem was:
1
2
3
4
5
6
7
8
9
10
11
12
f1(const int x)
{
         if(x==0)return 0;
         else {return 2*f1(x-1)+x*x;
          }
}
main()
{
      printf("%d", f1(5));
      
      while(1);
}


I tried to see whether 2*f1(x-1) is computed first, or f1(x-1 is.seems I were wrong.
(run my code as a C code)
cire (1853)
How could 2* the return value of f1(x-1) be calculated before the return value was calculated?



hooshdar3 (143)
not calculated, but parsed
cire (1853)
What does parsed mean to you?
Registered users can post here. Sign in or register to post.