basic recursion function question. typo in book?

This is a discussion on basic recursion function question. typo in book? within the C Programming forums, part of the General Programming Boards category; Hi all I am very new to C and am using Sams Teach Yourself C in 21 days. I have ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    4

    basic recursion function question. typo in book?

    Hi all I am very new to C and am using Sams Teach Yourself C in 21 days. I have come across a program in day 5 that has baffeled me. More specificaly, it is a recursive function that has confused me. The code is posted below. The book says that the function contiues to call itself until the if statement ( if a == 1) is true. To me, this seems likea typo in the book, and it wouldnt be the first one that I have come across. I say this because, if a does equal 1, then the function would return a value of 1, right? If this is indeed a typo, i would appreciate greatly if someone would explain how this function works for me. thanks a bunch))

    Code:
     
    unsigned int factorial(unsigned int a)
    {
        if (a == 1)
           return 1;
        else
        {
           a *= factorial(a-1);
           return a;
        }
    }

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    The last time the function is called, it does indeed return a 1, but since the last function call made was a *= factorial(a-1), you will just get the second-to-last returned value times 1, which of course is the second to last returned value.

    Start with a 4 (or some other relatively low number) and walk through it, writing down the values as you go. Its a good way to figure out how recursive functions work.
    Last edited by trippeer; 04-17-2005 at 07:45 PM. Reason: typo

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    It does call itself until a == 1. When a == 1 it returns 1, otherwise it calls itself, so the function calls itself until a == 1. I don't see the typo...
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You might want to add a printf call in the function to help you see whats happening.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    4
    ok i have figured it out now. thanks for yall's help. the working it out on paper and adding the printf statements did it.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    A more easier to see recursive factorial function that does the same thing would be something like:
    Code:
    unsigned int factorial(unsigned int a)
    {
            if (a == 1)
                    return 1;
            else {
                    return a * factorial(a-1);
            }
    }
    Here you can see the actual multiplication folding out, a * (a - 1) * (a - 2) * ... * 1. Just something extra
    The cost of software maintenance increases with the square of the programmer's creativity.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    The last entry for recursion in K&R-II explains all you need to know about recursion
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 9
    Last Post: 06-09-2008, 09:53 AM
  3. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 08:15 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21