0

Hey everyone I'm a beginner programmer and I have a problem with my recursive code to calculate the factorial of a number.

I get segmentation fault and I don't know why that's the case.

Any help would be very much appreciated :)

(In my code I'm trying to calculate the factorial of 4 for example)

#include <stdio.h>

int factorial(int i) {
    int result = i * factorial(i - 1);
    return result;
}

int main()
{   
    int result = factorial(4);
    printf("result is %d", result);
}   
kiner_shah
  • 3,939
  • 7
  • 23
  • 37
Joey
  • 1

1 Answers1

7

The problem is, you are missing the base condition. So your function is running forever until it uses all the memory available to your program and finally gives up giving the Segmentation fault error.

While writing the recursive functions you always have to provide a base condition which will stop the recursion. In your case it will be

if(i == 0)
    return 1;

Here you are basically saying that stop the recursion when i becomes 0.

Ezio
  • 2,837
  • 2
  • 29
  • 45
  • doesn't the recursive function terminate by itself when i==0 and return to the main function? – Joey Dec 29 '18 at 08:29
  • 1
    For you method to return to main function, you have to tell when to return? Try to think it through. In your case you are indefinitely calling the `factorial` method. You never said when to stop and thats the problem. Base condition tells your recursive function when to stop. Another note : Your recursive function will not directly return to main function it will return to the function which is present at the top of call stack. When all your recursive functions are returned and removed from call stack then only it will go to main function. – Ezio Dec 29 '18 at 08:34
  • 1
    @JomHo When `i` is zero, the line `int result=i*factorial(i-1) ;` is `int result=0*factorial(-1) ;` which calls `factorial(-1)` which calls `factorial(-2)` which calls `factorial(-3)` and so on. This causes a stack overflow as the function is called infinitely. – Spikatrix Dec 29 '18 at 08:34
  • @JomHo No. Why would -1 cause a crash? The crash is because of a stack overflow as the function gets called infinitely. – Spikatrix Dec 29 '18 at 08:37
  • ohh so the segmentation fault in that case is therefore an invalid read right? (because of the negative numbers) – Joey Dec 29 '18 at 08:37
  • 1
    Invalid read is little misleading, you get Segmentation fault when your program attempts to access an area of memory that it is not allowed to access. Since your recursive method eats up all the memory there is no memory left for your program to use. So as soon as you make the next recursive call (because you are never stopping) you will get this error. This might help https://stackoverflow.com/questions/2346806/what-is-a-segmentation-fault?rq=1 – Ezio Dec 29 '18 at 08:47
  • Jom Ho, please understand "infinite" and understand that each recursive call uses memory but the computer's memory is __not__ infinite. At some point your function wants to make another recursive call but your computer has no more memory to make the call. Then the operating system ends your program with a "Segmentation fault, core dumped". Your "_..an invalid read right? (because of the negative numbers)_" shows a lack of this understanding. Negative numbers have nothing to do with it. – Paul Ogilvie Dec 29 '18 at 09:05