Thread: function not returning correct values

  1. #1
    Registered User Paul Adams's Avatar
    Join Date
    Feb 2012
    Posts
    9

    function not returning correct values

    I'm a new at programming in C, and I seem to keep having syntax errors. I have written a function that tries to calculate the factorial of a number that the user entered into Main. Everything seems to work fine, except that I only get the number one (1) as an answer. I've pinpointed my problem to the for-loop within my function. Beyond this I can't seem to resolve the issue. Yes, I have worked on this for quite sometime now and yes it's part of my homework. I really want to know how to fix the problem, since I will have to fix on a test next time.

    Thanks for any help.

    Code:
    #include <stdio.h>
    
    
    int factorial(int);
    int f, i;
    int fact;
    
    
    int main()
    {
        //Input factorial value
        printf("This program finds the factorial of a number\n");
        printf("Number to be factored: ");
        scanf("%d",&fact);
    
    
        //Print the results of the Function
        printf("\n\n%d! equals: %d\n", fact, factorial(fact));
        getchar();
        getchar();
    }
    
    
    int factorial(int fact) { //function to find the factorial
        int answer = 1;
        /*for (i = fact; i <= 1; i = i -2) {
            answer = answer * (i * (i - 1));
        }*/
        return answer;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your loop condition is i <= 1. Therefore, if i is greater than 1, the loop will not loop even once. Now, if fact == 2, i = fact means i = 2, hence 2 <= 1 is false, so the loop never runs and answer remains 1.

    By the way, avoid global variables: get rid of f, declare i in factorial, and declare fact in main.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Paul Adams's Avatar
    Join Date
    Feb 2012
    Posts
    9
    so I changed my loop condition to be i > 1 and it works now. Thank you so much. I will fix the global variables as well. I was just moving them around earlier as a means of trying to troubleshoot my error.

    Thanks again!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning values from a function
    By stevenson in forum C Programming
    Replies: 14
    Last Post: 07-20-2011, 09:54 PM
  2. returning values from a function...
    By roaan in forum C Programming
    Replies: 1
    Last Post: 08-03-2009, 06:54 PM
  3. Returning Multiple Values from a Function
    By Programmer3922 in forum C Programming
    Replies: 4
    Last Post: 08-02-2008, 09:10 PM
  4. Returning Multiple Values from a function
    By Eddie K in forum C Programming
    Replies: 6
    Last Post: 03-12-2006, 01:18 PM
  5. c function returning multiple values
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-23-2001, 10:09 AM