• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Recursive function returning a string which is evaluated to give a result

silentwraith

New Member
Hi all, I am new to programming and I am trying to implement a simple calculator using VBA. The idea behind this function is to return a string. For example, the input string is [2,+,3,+,4,+,5] the output would be 14. I am not sure how can I implement this using the following recursive function. Please find below the code where I tried implementing the function. However, I am getting an error in "CalcTotal = CLng(Memory(Pos + 1)) & CalcTotal(Pos + 2)" saying that it is a type mismatch. ANy new suggestions are also welcomed.


Also, I have attached a link to my entire program below.


Code:
Function CalcTotal(ByVal Pos As Integer) As Long

    If Pos > CurrentPos Then ' Case 1: Nothing left to read
        CalcTotal = 0
        'return 0 as the result because there is nothing to do...
    ElseIf Pos = CurrentPos Then ' Case 2: There is only a number left
        CalcTotal = CLng(Memory(Pos))
        'return the number in the current position...
    Else ' Case 3: Read the next two slots of the array and combine with the rest of the array
        CalcTotal = CLng(Memory(Pos + 1)) & CalcTotal(Pos + 2)
        
    End If
End Function


Please find the entire code for reference.
 
Hi !​
As a beginner starter :​
Code:
Sub Demo1()
    MsgBox Evaluate(Replace("2,+,3,+,4,+,5", ",", ""))
End Sub
Do you like it ? So thanks to click on bottom right Like !​
 
Back
Top