Unable to return correct value of a variable in a recursive function

varunsinghal65

I am making following program to find factorial of a given number recursively.

#include<stdio.h>
int fact1=1;

int recursion(int x)
{
    if(x==0)
    {
        return fact1;
    }

    fact1=fact1*x;
    x--;
    recursion(x);   
}

int main()
{
    printf("%d",recursion(5));
    return (0); 
}

Problem :

The problem is that whenever I run this program it always gives me some garbage value 22752240 .

Thus Inorder to test my recursion I placed printf("%d",fact1) in the recursive function to check whether fact1 was 120 before being returned

int recursion(int x)
{
    if(x==0)
    {
        printf("%d",fact1);
        return fact1;
    }

    fact1=fact1*x;
    x--;
    recursion(x);   
}

and when I executed program I got this 120 22752240 as output, meaning that recursion() was printing correct value of fact1 but was unable to return correct value of fact1.

Can anyone explain why is this happening?

Sourav Ghosh

Sure. you forgot to return a value at the very the end of recursion(). Without that return statement, the use of recursion() as an argument ot printf() invokes undefined behaviour.

To elaborate, only for the case when x == 0 is TRUE, you're using a return statement to return a value. In other scenario, you're missing a return statement. You need to add return statement for other case also.

Reference: Form C11 standard, chapter §6.9.1, Function definitions

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

That said, the recommended signature of main() is int main(void).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does this recursive function return the correct value?

Recursive function is able to print, but does not return the correct value

Swift doesn't return the correct value in recursive function

How to make a recursive fib-function return the correct value with memoization

Php recursive function return null while variable have value

How to save the return value of a recursive function in a variable JavaScript

Unable to return the correct result of the function

How does this recursive function return the correct answer?

Return correct value from recursive indexOf

Value of variable on recursive calling of a function

Variable in recursive function keeps it value

Unity-How to make recursive function return correct value that only if condition is true

return function value to variable

Unexpected return value from recursive function

Recursive Function return value in vb script

How to terminate a recursive function and return a value

String return value in recursive function in java

how to return a value from recursive function in python

How does recursive function return its value?

recursive function cannot return value as expected

Return value from Linux bash recursive function

Undefined return value mongoose / nodejs recursive function

Why does this recursive function return the wrong value?

Correctly return Promise value from recursive function

Final Value of a Variable in a JavaScript Recursive Function call

Keeping the initial value of a variable in recursive function

Store Function Return Value into a Variable

aggregate function return correct value after a while

Lambda function does not return correct value