+ Reply to Thread
Results 1 to 2 of 2

Delineating recursive function call in emulation code of worksheet SUM function

  1. #1
    Registered User
    Join Date
    09-27-2021
    Location
    Washington, DC
    MS-Off Ver
    365 (64 bit)
    Posts
    11

    Angry Delineating recursive function call in emulation code of worksheet SUM function

    Currently working through the book Power Programming with VBA for Excel 2019. Frankly, up until a few chapters in, they are leaving a lot to be desired when it comes to explaining the more advanced statement concepts in their sample codes. But on to my point: Could you please explain each step of the recursive call statement MYSUM = MYSUM(MYSUM, n(m))

    When MYSUM is initially recursively called, the initial MYSUM argument value is 0, since it was initialized as being =0 in the assignment statement at the beginning. But doesn't the same occur in each subsequent recursive call? So each call is just adding the next call's return value to zero. I do not grasp the compounding that is supposed to occur, mainly due to the initialization of MYSUM as zero during each call of the function.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,220

    Re: Delineating recursive function call in emulation code of worksheet SUM function

    Quote Originally Posted by hic24 View Post
    But doesn't the same occur in each subsequent recursive call? So each call is just adding the next call's return value to zero.
    Each function call starts with the zero value of this function (in this case Empty, which will be converted to 0). Summation occurs by passing arguments to a function. When you call a function recursively, you still pass arguments. In the line
    Please Login or Register  to view this content.
    you pass the calculated value of the function for this moment of computation and the array of successive values. So you passed two arguments that will eventually be summed up.
    For you, I was analyzing such a sheet function
    Please Login or Register  to view this content.
    The test showed that in this code snippet:
    Please Login or Register  to view this content.
    should be changed to
    Please Login or Register  to view this content.
    Perhaps this fragment needs to be further developed to handle two- and multidimensional arrays.

    In order to understand in depth the operation of the function, I propose to analyze it in step mode.
    1. Insert a breakpoint in the lines:
    • For i = 0 To UBound(args)
    • (after Case "Variant()") n = args(i)
    • (after Case Else) MYSUM = MYSUM + args(i) and
    • End Function
    2. In the Watches window, add the watches you want to watch: MYSUM, args, and n
    3. Now insert i.n.t.o any cell as a sheet function (i.n.t.o = into)
    Please Login or Register  to view this content.
    4. Go through the execution in steps (F8) and observe the watches values.

    Do this patiently until the end of the function. I think then you will understand how it works.

    Artik
    Last edited by Artik; 09-29-2021 at 10:45 AM. Reason: fight with Sucuri

+ 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. Call Specific worksheet function from ThisWorkbook module
    By losekiss431a in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 07-21-2015, 05:17 AM
  2. Some One Help me write Recursive function on my Code
    By jatin89 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-25-2013, 07:40 PM
  3. [SOLVED] how to call a function, and send across a worksheet object
    By aaronkoh in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-27-2012, 04:49 AM
  4. Call a sub or function from another worksheet sub
    By srinivassurapareddi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-16-2012, 09:08 AM
  5. Recursive Worksheet Function for Parsing Text
    By Carnifex930 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-04-2010, 03:31 AM
  6. how can i call sub or function in vba code
    By xxx in forum Excel General
    Replies: 3
    Last Post: 06-28-2006, 06:50 AM
  7. how to make sure which worksheet call the function
    By lvcha.gouqizi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-21-2005, 04:05 PM

Tags for this Thread

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