Why doesn't a recursive program work?

DavidSCowan

Board Regular
Joined
Jun 7, 2009
Messages
78
Hi

I am trying to learn about recursive programs but when I write a really simple one - to sum numbers up to n - I get #VALUE! error. Can someone please tell me what i am doing wrong.
Function mySum(n)
Dim n As Integer
Dim result As Integer
If n <= 1 Then
result = 1

Else: result = n + mySum(n - 1)
End If
mySum(n) = result
End Function

Thank you
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
an observation i make is that in the function declaration, you pass the variable 'n' (as varient by default)
but then inside the sub, you Dim n as integer, which has 2 effects. firstly n is zero'd and i suspect that the change of type has caused the error?
 
Upvote 0
You have a duplicate declaration of n and your return value is incorrect. The code should be more like:
Code:
Function mySum(n)
   Dim result                 As Integer
   If n <= 1 Then
      result = 1
   Else
      result = n + mySum(n - 1)
   End If
   mySum = result
End Function
 
Upvote 0
an observation i make is that in the function declaration, you pass the variable 'n' (as varient by default)
but then inside the sub, you Dim n as integer, which has 2 effects. firstly n is zero'd and i suspect that the change of type has caused the error?

Hi Diddi
Thank you very much for your reply and pointing out the dim errors. (To be honest I only put in the dim statements in the post in case in case I was told off for not having them). When they aren't there it doesn't change the #VALUE! error.

Any other ideas would be gratefully received.

With kind regards

David
 
Upvote 0
Hi Rory

Thank you very much for your reply but I am afraid your suggestion doesn't work either. If you had any other thoughts they would be gratefully received.

With kind regards

David
 
Upvote 0
Hi Rory

Thank you very much for your reply but I am afraid your suggestion doesn't work either. If you had any other thoughts they would be gratefully received.

With kind regards

David
It works for me for reasonable values of n.
Are you still getting a #VALUE! error?
 
Upvote 0
Hi circledchicken

Great that it works for you but I am afraid I still get #VALUE! error. And I am only trying it out on low numbers e.g 4.

How strange!

One of the classics for illustrating recursion is for factorial n. The very similar code (result = n * myFactorialRecussive(n - 1) works okay for me here but not the sum of digits.

Any thoughts appreciated.

David
 
Upvote 0
Rory's code works for me as well, copy/pasted into a module in the workbook. The code as shown handles numbers up to 255, so I'm not sure why you're getting a #VALUE error when trying out 4... Where do you have your code saved at, and how are you testing it in your workbook?
 
Upvote 0
Hi Andrew

Thank you for trying it out. I am stumpted. it ought to work and it works for you and Circledchicken but not for me. I am pasting in Rory's version but still the error! What on earth could be wrong?
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top