+ Reply to Thread
Results 1 to 3 of 3

Recursive factorial function

  1. #1
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Recursive factorial function

    Hi,

    Trying my hand at recursive functions and found a simple recursive function for factorials and found this online :

    Please Login or Register  to view this content.
    I don't understand how this works. When num eventually reaches 1 won't the function return 1 instead of the complete factorial ?

    Thanks,
    amphi

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,408

    Re: Recursive factorial function

    A recursive function repeatedly calls itself, so there needs to be some way of stopping this. Suppose the function is called with a value of 3 - then the first bit is ignored and the line:

    myfact = num * myfact(num - 1)

    is executed. However, this calls the function again with a reduced number, so that particular execution is suspended while it tries to evaluate

    myfact(2)

    This again will reach the line:

    myfact = num * myfact(num - 1)

    and needs to get a result from

    myfact(1)

    This indeed returns 1, so the previous line can be executed (i.e. 2*1) and then the fist call can also be executed (i.e. 3*2*1).

    Hope this helps.

    Pete

  3. #3
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Recursive factorial function

    Thanks a lot Pete. Somehow couldn't wrap my head around the recursive function & base case. I assumed that each time the function called itself it "lost" the previous call result as I thought a variable was needed when it doesn't.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Excel Recursive function or not..! Pl help
    By xsmash in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 09-13-2013, 09:17 PM
  2. Factorial Function
    By Microsoft Excel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-15-2013, 02:00 PM
  3. writing a recursive function
    By rmoney in forum Excel General
    Replies: 6
    Last Post: 07-19-2010, 02:59 PM
  4. [SOLVED] Factorial (like =FACT) function?
    By mr tom in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 06-12-2006, 08:50 PM
  5. Range of Factorial Function
    By Rushi in forum Excel General
    Replies: 7
    Last Post: 09-16-2005, 08:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1