Factorial function in C does not return the result

Asked

Viewed 3,951 times

3

What’s wrong with my code?

int fatorial (int n, int num, int fat) 
{
    if(num >= 0)
    {
        fat = 1;
        n = num;

        while(n > 0)
        {
            fat *= n;     //FATORIAL = FATORIAL*N
            n--;
       }
    }
}
  • 3

    Explain the function parameters, please. Why there are three, and two of them you change the value within the function without passing the variable by reference (that doesn’t even make sense, in my view)? In fact, the function has no return.

  • You do not need to pass a " variable " to store the factorial value ( up because if you were to do this it would have to be a pointer and as it is factorial you would normally just give a Return in the value ), you only need to do an if / Else ' if ' the number is less 2 Return 1 and on ' Else ' you call the function by multiplying it by itself minus one.

2 answers

4


The function is not returning anything, whether the value is valid or not. I used -1 to indicate error.

Also, you are using completely unnecessary parameters: fat can be local and n simply do not even need as a local variable.

#include <stdio.h>

int fatorial(int num) {
    if (num >= 0) {
        int fat = 1;
        while (num > 0) {
            fat *= num;
            num--;
        }
        return fat;
    } else {
        return -1;
    }
}

int main(void) {
    printf("%d\n", fatorial(0));
    printf("%d\n", fatorial(1));
    printf("%d\n", fatorial(5));
    printf("%d\n", fatorial(-5));
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • I can do it on main:

  • int main(){ int n,num,fat; printf("THIS POGRAM CALCULATES N FACTORIAL = N! n"); printf("Enter a value to be calculated: "); scanf("%d",&num); printf(" n to factorial of %d = %d n",factorial); ///printf("%d!=%d n",fat); //Else printf("Factorial calculation applies only to natural numbers. n"); system("pause"); //pause system }

  • @Thiagoferreira can, of course, just test if returned -1. Although you can make negative factorial.

  • There is no negative factor, because I want the user to type the factor and display on the screen for that need to call the function is not? Only it is giving error in main.

  • 1

    @Thiagoferreira I answered what was in your question. If you have another problem that is not in the question you should open a new question by putting the code that is giving error and giving as much information as possible to help better. See how to make a [mcve].

2

By default, C returns 0 for a function without Return, for example the function int teste() { } no build error, because the C language returns zero as a default Return. Then, the function becomes:

int teste() {
  return 0; // código adicionado pelo compilador C
}

In your job:

int fatorial (int n, int num, int fat) 
{
    if(num >= 0)
    {
        fat = 1;
        n = num;

        while(n > 0)
        {
            fat *= n;     //FATORIAL = FATORIAL*N
            n--;
        }
    }
    return 0; //adicionado pelo compilador
}
  • and the main how it gets? the user typing the number of 1 until 10 informing the factorial?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.