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 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