-1

In this factorial recursion "printf" outputs 2 6 8, Can't understand why...

  #include <stdio.h>

int f(int n)
{
    int k;
    if (n==1)
        return 1;
    else
        k = n*f(n-1);

  printf("%d ",k);

}

int main()
{
    f(4);
}
Alon T
  • 27
  • 5
  • `main.cpp: In function 'int f(int)': main.cpp:11:11: warning: control reaches end of non-void function [-Wreturn-type]` – tkausl May 19 '18 at 13:59
  • 1
    You're not always returning a value from `f` - that's undefined behavior (at least in C++, but probably also in C), and the code could print anything – hlt May 19 '18 at 13:59
  • Sorry my bad, the code was written in C not C++ – Alon T May 19 '18 at 14:01
  • What was your intention with `return k = n*f(n-1)`? – vgru May 19 '18 at 14:02
  • it's printing 2 6 24 – Hesam Faridmehr May 19 '18 at 14:02
  • you dont need `k`, just `return n*f(n-1)`. You should also have `if(n <= 1)`, just in case – Mitch May 19 '18 at 14:02
  • You can't print after you return. – DimChtz May 19 '18 at 14:04
  • Post a [mcve]. This is not your actual code, even after several edits. In other words, if you want people to tell your what's wrong with your code, then post your actual code. – vgru May 19 '18 at 14:04
  • If you have code that is printing "2 6 24", then it is not the code that is posted in this question. The call of `printf()` in `f()` will never be reached. Printing code that doesn't exhibit the behaviour you describe is rarely effective if you want help. Voting to close, accordingly. – Peter May 19 '18 at 14:08
  • @AlonT You are still in the process of changing the question, including changes which break and/or modify the behaviour of your code. For example, in the currently shown version of the code, the function `f()` has paths which end without a return. Please delete the question while you are editing it and undelete it when you are done. Until then, the question is considered anywehere between off-topic for being unclear, for not having a MCVE or for not being reproducable. – Yunnosch May 19 '18 at 14:18
  • If this is the final code, then the actual question is "Why does it work without `return`?" and is a duplicate of https://stackoverflow.com/questions/4644860/function-returns-value-without-return-statement – Yunnosch May 19 '18 at 14:24

2 Answers2

1

The original version of the code in the question was:

int f(int n) {
    int k;
    if (n == 1)
        return 1;
    else
        k = n * f(n - 1);

    printf("%d ", k);
}

int main() {
   f(4);
}

This code has undefined behavior because you do not return a value properly if n != 1, causing the calling code to use an unpredictable value in its own calculation. The behavior is undefined, anything can happen.

Adding the return statement fixes this issue. Note however these extra points:

  • variable k is useless in the f function.
  • f should return 1 for an argument of 0.
  • you should output a newline after the number and return 0 in main.

Here is modified version:

#include <stdio.h>

int f(int n) {
    if (n <= 1)
        return 1;
    else
        return n * f(n - 1);
}

int main() {
    int i;
    for (i = 0; i < 10; i++) {
        printf("%d! = %d\n", i, f(i));
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
1

There are many mistakes you are making here:

printf("%d ",k);

this line is never going to be executed because in any case if() else clause will return before it.

  • You fell victim to a "moving target question". You might want to review your answer, which was invalidated by OPs edits to the question. Because of this, you are at risk of being downvoted by future readers for not answering the questin as it now is - even though it is not your fault. (I did not by the way.) Be careful. Note that there is at least one version of the edited question in which your answer made sense and was pointing out one of the problems. As I said .... not your fault. – Yunnosch May 19 '18 at 14:26
  • Thanks yunosch. I will delete this answer –  May 19 '18 at 14:30