helpmepleaseibad
helpmepleaseibad

Reputation: 29

Can someone explain to me why my factorial recursion code can't be compiled

i have made a simple program to calculate the factorial of a number. Below is the code.

#include <stdio.h>

int factorial(int i);

int main(void)
{
    int i;
    printf("Factorial of: ");
    scanf("%i", &i);
    printf("Answer: %i\n", factorial(i));

}

int factorial(int i)
{
    if (i == 0)
    {
        return 1;
    }

    factorial(i) = (factorial(i - 1) * i);
    return factorial(i);
}

the compiler told me the following line has issues

factorial(i) = (factorial(i - 1) * i);

so i changed the above line to the following and it worked

int a = (factorial(i - 1) * i);
return a;

So, can someone explain to me why the initial line which is (factorial(i) = (factorial(i - 1) * i);) didn't work?

Upvotes: 0

Views: 109

Answers (4)

Alex Lop.
Alex Lop.

Reputation: 6875

factorial(i) = (factorial(i - 1) * i);

This line is invalid in terms of C standard. It is related to the lvalue and rvalue definition.

lvalue - is an expression referring to an object.The name 'lvalue' comes from the assignment expression E1 = E2 in which the left operand E1 must be an lvalue expression.

rvalue - is an expression which is not lvalue (I couldn't find the exact definition of it). In other word, rvalue cannot be re-assigned.

Example:

int n;
...
n = 3; // --> Legal, n is an lvalue and 3 is an rvalue
3 = n; // --> illegal, 3 is rvalue and thus n cannot be assigned to it

Another example

int a, b, c;
...

a = b + c; // --> Legal since 'a' is an lvalue (refers to an object/memory)
/* Note that 'b' is also lvalue, 'c' is also lvalue BUT 'b + c' is an rvalue expression! */
/* the temporal storage allocated for the result of the expression 'b + c' cannot be "visible" */
/* one cannot check the address of such expression: &(a + b) ==> illegal */
b + c = a; // --> Illegal

In you example, factorial(i) represents the return value of the function which is an rvalue.

For more details: https://www.embedded.com/lvalues-and-rvalues/

Upvotes: 2

Nick Gkloumpos
Nick Gkloumpos

Reputation: 151

factorial(i) = (factorial(i - 1) * i);

You are trying to assign an integer on the call of a function. When you write factorial(i) you are telling the system to call the function factorial and return a value. You can't assign a value to that value.

Upvotes: 1

Hellmar Becker
Hellmar Becker

Reputation: 2982

It's because in this snippet

    factorial(i) = (factorial(i - 1) * i);
    return factorial(i);

in all the places where factorial(i) is mentioned, it would always be interpreted as trying to call the function once more.

To fix it, introduce a temp variable:

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

Upvotes: 1

backspace
backspace

Reputation: 134

Simply because you cannot give a value to a function call in C. factorial(i) calls the function (which would, by the way result in an infinite recursion) and your were trying to assign a value to that call, which is not possible. In some other languages this is the way to return a value, not in C though.

Upvotes: 1

Related Questions