Recursive Function

tooltiperror

Super Moderator
Reaction score
231
How would I make a recursive function?

Like if I wanted it to keep calling itself so I don't have to use loops.
 

tooltiperror

Super Moderator
Reaction score
231
JASS:
library

	globals
		trigger Trigger=CreateTrigger()
		private integer i=0
	endglobals
	
	private function Register takes nothing returns nothing
		call TriggerRegisterPlayerUnitEvent(t,Player(i),EVENT_PLAYER_UNIT_WHO_KNOWS,null)
		set i=i+1
		if i<12
			call ExecuteFunc("Register")
		else
			call TriggerAddAction()
		endif
	endfunction

endlibrary

// more efficient than:

	private function Register takes nothing returns nothing
		local trigger t=CreateTrigger()
		local integer i=0
		loop
			call TriggerRegisterPlayerUnitEvent(t,Player(i),EVENT_PLAYER_UNIT_WHO_KNOWS,null)
			set i=i+1
			exitwhen i==12
		endloop
	endfunction
	
// ?


I ask because I am interested in how J4L registers events without loops, using pointers somehow.
 

DioD

New Member
Reaction score
57
what your reasons to interest?

if you about thread limit, yes it just for heavy calculations thread from thread.
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Benchmark it ;)

But I don't see a reason for why executing a function would be less heavy than a simply loop... :p
 

tooltiperror

Super Moderator
Reaction score
231
Because people say that loops are 'bad' :p
 

tooltiperror

Super Moderator
Reaction score
231
No one did.

But I am assuming in certain conditions, recursion is more efficient.
 

DioD

New Member
Reaction score
57
then you will lead board of shame, recursion never effective then loops.
 

Xienoph

You can change this now in User CP.
Reaction score
43
Yeah. Recursive functions are at most as efficient as loops.
 

Hatebreeder

So many apples
Reaction score
381
then you will lead board of shame, recursion never effective then loops.

We do recursive because we can. Besides that, I think it's quite cool =P

Not to mention, that when you program something else than vJass and have a different engine, it comes in quite handy because (i = i + 1) in greater numbers can be slower than ( call Exec(...) ); Besides, you "iterate" at the bottom of your script.

Anyways, I'm not very sure if it's slower or faster or whatever, I've only heard people claiming it to be better in certain situations ( such as doing stuff with large indexstructures ). Correct me if I'm wrong.
 

Bankde

Member
Reaction score
20
In fact, when the computer is compute the output. General Function is better than Loop and Loop is really better than recursive. (As loop is the recursive itself but more simple)

The only reason we use loop is to minimize the code.

We only use recursive function when we need something that loop can't do :
Example in C++ language

Code:
int sumsquare(int i)
{
	if(i <= 1)
		i=1;
	else i = i*i + sumsquare(i-1);
	return i;
}

Here you will recieve the output as the Sum of all square which Loop can't do it.

I do not recommend recursive function which is hard, have to check index passed the function (while loop use one local variable) and risk of infinite loop.
 

tooltiperror

Super Moderator
Reaction score
231
Actually, infinite loop is the only valid use of a recursive function in JASS I can think of.

JASS:

//! zinc

library LoopTest
{
    function onInit()
    {
        local integer i=0
        while(i&gt;-1)
        {
            i=i+1;
            BJDebugMsg(I2S(i));
        }
    }
}

//! endzinc


Of course, it only gets up to somewhere around 800-900, and then WC3 ends the loop. If you need to loop the entire game, you need to use recursion instead, or a repeating timer. I'm willing to guess a recursive function is better than a Timer, but no evidence to support it.

Other than very large loops, I think recursion is useless.
 

DioD

New Member
Reaction score
57
warcraft will run recursion function as fast as it can.

with execute it will end in warcraft close without errors, and you wont get chance to see how much it got

with triggers it will end in warcraft close with error same as above

with timer it will end with lockdown and you will reboot your PC (vista\seven will kill warcraft soo you dont need to reboot)

with direct call or loop it will end with thread break and major memory leak but you will be able to check how much times loop executed.

simple loop will end nearly 16000, its possible to fill array with values in one run.
 

saw792

Is known to say things. That is all.
Reaction score
280
I use recursion for tree data structures. It's really the only proper way to implement them in wc3 (although I don't find them useful very often). The ExecuteFunc() version of recursion is pretty useless other than avoiding the op limit, things like what Bankde posted are a more usable form. We need some benchmarks for a (reasonable implementation of a) recursive data structure with loops and recursion.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Actually, infinite loop is the only valid use of a recursive function in JASS I can think of.

What?

Other than very large loops, I think recursion is useless.

I think I must be missing something from the thread. Recursive functions are more useful than loops in some occasions, in which it simplifies the logic (for the programmer). Using ExecuteFunc is only necessary for bypassing the op limit, in any other case, this compiles and runs fine:

JASS:
function Factorial takes integer i returns integer
    if i &lt;= 1 then
        return 1
    else
        return i*Factorial(i-1)
    endif
endfunction


Unless we're talking about bypassing the operation limit, the use of recursion or loops is entirely up to the coder, as there isn't a big difference in speed. Generally loops will be faster, since they don't have the overhead of a function call.
 

D4RK_G4ND4LF

New Member
Reaction score
1
loops are much better
every parameter decreases the operation limit while loops can use the same local variables again and again

JASS:
//&lt;16500
function a takes integer i returns integer
    if i&gt;udg_i then
        return i
    endif
    return a(i+1)
endfunction

//&lt;14500
function a takes integer i, integer c returns integer
    if i&gt;udg_i then
        return i
    endif
    return a(i+1, c)
endfunction

//udg_i = 42857
loop
    set udg_i = udg_i + 1
endloop

//udg_i = 23076
loop
    exitwhen udg_i &gt; 10000
    set udg_i = udg_i + 1
endloop

//udg_i = 33333
loop
    exitwhen false
    set udg_i = udg_i + 1
endloop


speed (measured with a manual stop watch)
JASS:
//10k executions take 8 sec
function a takes integer i returns integer
    if i&gt;1000 then
        return i
    endif
    return a(i+1)
endfunction

//10k executions take 4 sec
function b takes integer i returns integer
    loop
        exitwhen i&gt;1000
        set i = i + 1
    endloop
    return i
endfunction
 

saw792

Is known to say things. That is all.
Reaction score
280
I think we already established that loops are going to be more efficient than a recursive function. The reason we use recursive functions is to perform tasks that would be complex or tricky to do in a loop, or operations on data structures that are suited to a recursive approach (e.g. trees).

An actual comparison between an actual, legitimate use of recursion and it's equivalent loop structure would be nice to see. Since we generally use loops by default it's not valid to compare a loop to a recursive function when the use of a recursive function doesn't make sense.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/
  • The Helper The Helper:
    I think we need to add something to the bottom of the front page that shows the Headline News forum that has a link to go to the News Forum Index so people can see there is more news. Do you guys see what I am saying, lets say you read all the articles on the front page and you get to the end and it just ends, no kind of link for MOAR!
  • The Helper The Helper:
    Happy Wednesday!
    +1
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    Sticking with the desserts for now the latest recipe is Fried Apple Pies - https://www.thehelper.net/threads/recipe-fried-apple-pies.194297/
  • The Helper The Helper:
    Finally finding about some of the bots that are flooding the users online - bytespider apparently is a huge offender here - ignores robots.txt and comes in from a ton of different IPs

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top