zaedric
zaedric

Reputation: 45

c function not being called?

I have basically no experience in c, so i'm not sure why this isn't working.

#include <stdio.h>

int a=1; 
int b=5;

void fact(a,b) {

    if(b == 1) {
        return;
    } else {
        a = a * b;
        b = b - 1;
        fact(a, b);
    }
}

int main() {

    fact(a, b); 
    printf("%d", a);
}

The program should call fact recursively until a becomes 120. However it prints 1. I'm not sure why this is happening so any help would be appreciated.

Upvotes: 2

Views: 4644

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310910

This function definition (provided that the compiler accepts it)

void fact(a,b) {

if(b == 1) {
    return;
} else {

    a = a * b;
    b = b - 1;
    fact(a, b);
    }   
}

looks in fact the following way

void fact(a,b) 
int a, b;
{

if(b == 1) {
    return;
} else {

    a = a * b;
    b = b - 1;
    fact(a, b);
    }   
}

That is the variables a and b in the function definition are local variables of the function that have copies of the arguments supplied to the function by this call

fact(a, b);

Thus in the function call there are used global variables a and b but inside the function itself there are used local variables a and b that is copies of the global variables. Any changes of these local variables do not influence on the global variables.

How to resolve the problem?

The first approach is to remove the function parameters. In this case a and b inside the function definition will denote the global variables

void fact( void ) 
{

if(b == 1) {
    return;
} else {

    a = a * b;
    b = b - 1;
    fact(a, b);
    }   
}

and you will get the expected result.

The second approach is to use parameters but also to return the result. For example

int fact( a, b) int a, b; { if(b == 1) { return a; } else {

a = a * b;
b = b - 1;
return fact(a, b);
}   

}

and call the function the following way

a = fact( a, b );

However such a function definition is deprecated.

It would be better to write it like

int fact( int a, int b) 
{
if(b == 1) {
    return a;
} else {

    a = a * b;
    b = b - 1;
    return fact(a, b);
    }   
}

And the third approach is to pass arguments to the function by reference. For example

void fact( int *pa, int *pb ) 
{

if( *pb == 1) {
    return;
} else {

    *pa = *pa * b;
    *pb = *pb - 1;
    fact(pa, pb);
    }   
}

and call the function like

fact( &a, &b );

However it would be simpler to write the function the following way

int fact( int n )
{
    return n < 1 ? 1 : n * fact( n - 1 );
} 

and call it like

a = fact( b );

Upvotes: 0

&#181;tex
&#181;tex

Reputation: 908

You are passing copy of variables a and b. Pass Variables as a pointer, if you want to update the value . Modified the code below -

void fact(int *a, int *b) {

    if(*b == 1) {
        return;
    } else {

        *a = *a * *b;
        *b = *b - 1;
        fact(a, b);
        }
    }

    int main() {

        fact(&a, &b);
        printf("%d", a);
    }

You can visit the below link -

How to update the value in another function

Upvotes: 4

Related Questions