please help me understand this vba code

mukudu99

Registered User.
Local time
Today, 10:09
Joined
Dec 22, 2006
Messages
15
Hi VBA Gurus
couls anyone kindly explain to me what this code does if suppliied with the following values:1,2,3,4,5

'example on passing arguments byref declaration
Function test2(a As Integer)
a = a - 2

If a = 0 Or a < 0 Then
test2 = 2
Exit Function
End If
test2 = test2(a) * (a + 10)
End Function
Sub calc2()
Dim aa As Integer
aa = InputBox("enter integer ", "integer", 0)
MsgBox aa
MsgBox test2(aa)
MsgBox aa
End Sub

i can not seem to figure out how the outcomes are being calculated

thanks
 
it is a recursive function so it calls itself until a = 0 or less.
run it one line at a time by pressing F8 and hover the mouse over 'a' at each step and you should be able to follow what is happening.

Peter
 
A good example of recursive programming is fibonacci numbers

ie 1 1 2 3 5 8 13 21 34 55 etc (as shown in da vinci code)

each number is sum of previous 2 terms. incidentally this sequence is very important and interesting mathematically - one example is that the ratio of successive terms more nearly approaches golden mean, as sequence continues.

since each term is sum of previous 2 terms we know that fib(9) is the sum of fib(8) and fib(7), but we also know that fib(8) is the sum of fib(7) and fib(6) etc

so we can very elegantly (with recursion) write a function to determine a fibonacci number, as in the example below. Using a non-recursive function would take many more lines of code. HOWEVER, although recursive functions are often elegant and easy to write, the language behaviour is very complex. each call of the recursive function has to be pushed and popped from the stack - so using recursion for very deep functions can become slow and inefficient. eg try using the recursive fibonacci function for values above 30, and see how how long it takes to resolve. Note that not every language supports recursive programming.


Code:
Function fibno(index As Long) As Long
If index <= 2 Then
    fibno = 1 'this stops the recursion
Else
    fibno = fibno(index - 1) + fibno(index - 2)
End If
End Function


Sub tryit()
MsgBox (fibno(20))
End Sub
 
your example may have been used to demonstrate the difference between a parameter passed byref and byval, rather than recursion

with a byref parameter, the actual address of the parameter is passed and the called function can therefore MODIFY the passed parameter.

with a byval parameter, a copy of the parameter is passed and the called function can therefore only modify the copy, and not the original parameter.

for some reason access vba passes by reference as a default, which I believe is somewhat unusual - it is much safer for a programming language to pass byvalue as a default, to avoid inadvertent side effects caused by modifying passed parameters.

I don't know if there is a way of setting the default behaviour to be byval instead of byref.
 
thanks gurus
Bat17

I like the f8 mouse hoover technique. it really shows what is happening.

thanks gemma-the-husky for the explanation it reminds me of the days when i was cracking maths sequences and the like

thanks a million i am trying to go for Access VBA power programming status

thanks again
 
If you like F8 to step through, look into the "Watch Window" (in the View drop-down menu). This allows you to track multiple variables at once as you step through the code. To add a watch, simply right-click on the variable you want to watch and select "Add Watch". It will be placed in the watch window. This way, when you have multiple variables that you want to track, this window will update in real-time as you step through the code. It's a very handy debugging tool.

~Moniker
 

Users who are viewing this thread

Back
Top Bottom