function calling itself to countdown

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Sem552
Posts: 4
Joined: 24 Mar 2024, 06:47

function calling itself to countdown

16 Apr 2024, 13:27

Hi

Recently came aboard on the AHK train. was able to translate a script to V2.
now i am expanding this script and find an issue i cannot get my head around.
its a function with an "amount" as parameter, and counts down the amount by one and calls itself again, until 0 is left and then the function ends with no output.

Code: Select all

Amount := 2
rundown(Amount){
    while (Amount > 0){
        Amount := Amount - 1
        rundown(Amount)
    }
}
msgbox Amount
I would expect the result being 0, but the msgbox always shows 2.
Anyone can give a hint what i am missing?

Cheers,
Sem
User avatar
boiler
Posts: 16997
Joined: 21 Dec 2014, 02:44

Re: function calling itself to countdown

16 Apr 2024, 16:07

A few issues: You define a function, but you never call it. Then you have a while loop within a recursive function, so each instance is waiting for the value to become 0. The recursive quality is already a loop, so you don't need a while loop. It would also be good not to name the global and local variables as the same thing so there is no confusion (to you, the author) that they are actually not the same variable. Seems like you are trying to do this:

Code: Select all

Amount := 2
Amount := rundown(Amount)
msgbox Amount

rundown(a) {
		a := a - 1
		if (a > 0)
			a := rundown(a)
		return a
}

This version lets you track how many times it's called:

Code: Select all

rAmount := 2
Amount := rundown(Amount)
msgbox Amount

rundown(a) {
		MsgBox 'Local value: ' a
		a := a - 1
		if (a > 0)
			a := rundown(a)
		return a
}
teadrinker
Posts: 4336
Joined: 29 Mar 2015, 09:41
Contact:

Re: function calling itself to countdown

16 Apr 2024, 18:12

In addition to @boiler's answer, AHK v2 syntax allows you to simplify this function:

Code: Select all

MsgBox rundown(2)

rundown(a) => --a ? rundown(a) : a
or

Code: Select all

MsgBox rundown(2)

rundown(a) => (MsgBox(a), --a ? rundown(a) : a)
Sem552
Posts: 4
Joined: 24 Mar 2024, 06:47

Re: function calling itself to countdown

18 Apr 2024, 05:23

Thanks both for your responses.
After posting, I played around more and realized i didn't call my function. but couldn't update my pending post :), when i did call the function it had the same behavior.
This example code is an abstract from a bigger function. There a lot going on before it decides so subtract one.
I could use a Loop and call the function with the original number if it shouldn't subtract one, but i couldn't find a way to prevent an infinite loop other than nest it in another Loop.
i settled for "While" with 2 conditions.

Great suggestion in using different variable names within the function and outside the function.
the problem the described issue causes is that when the function is done, its recalled with the original amount.
Changing the variable name in my function, I realize my initial code abstract doesn't exactly represent the code.
"Amount" is never used outside of the function.

Nice to see ahk supports --a and ++a. I'll use that too.

Still your suggestion is really helpful. It didn't point to some limitation within ahk, the limitation is my coding skills, not the language :)
something else popped up after moving my script to another machine. that needs fixing first (OCR not working on a new pc).

Thanks again, Sem

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: kunkel321, mikeyww and 43 guests