SA-MP Forums Archive
їRecursion? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: їRecursion? (/showthread.php?tid=645863)



їRecursion? - ByMatt20030 - 05.12.2017

Why if i script this:

Code:
forward Test();
public Test()
{
    new Random;
    Random = random(1);
    if(Random == 0) return Test();
    return 1;
}
I get this warning:
warning: recursion in Test

Shall i worry about that? or it's simplily a normal warning to the writer?

Thaks!


Re: їRecursion? - Kaperstone - 05.12.2017

Because recursion is when you recall the function inside itself.

You're creating an infinite loop which might freeze your server until it reaches an end point or you close the server successfully.

Shall you be worried about it? pretty much, if you don't stop it correctly.


Re: їRecursion? - ByMatt20030 - 05.12.2017

So if i had bad luck, and the function always returns inside itself(in case of that code for example) because of the random value.. my server can crash. Ok...



Thank you a lot!


Re: їRecursion? - jasperschellekens - 05.12.2017

Quote:
Originally Posted by ByMatt20030
View Post
So if i had bad luck, and the function always returns inside itself(in case of that code for example) because of the random value.. my server can crash. Ok...



Thank you a lot!
Not because of the random value...
Code:
    if(Random == 0) return Test();
Quote:
Originally Posted by Kaperstone
View Post
You're creating an infinite loop which might freeze your server until it reaches an end point or you close the server successfully.
The return Test(); part does create this infinite loop kaperstone is talking about.


Re: їRecursion? - chneubeul - 06.12.2017

i compiled that, i didn't get any warning xD


Re: їRecursion? - adri1 - 06.12.2017

Code:
randomEx(max, exception)
{
	new rand = random(max);
	while(rand == exception)
	{
		rand = random(max);
	}
	return rand;
}
Be careful, don't make an infinite loop


Re: їRecursion? - chneubeul - 06.12.2017

Quote:
Originally Posted by Y_Less
View Post
This is a non-warning added in certain versions of the compiler and not a problem.
How can we have a different version of compiler by using the same archive ?


i have test this :


Code:
func()
{
	called++;
	printf("called %d times.", called);
	return func();
}
result is :
[13:15:18] called 1358 times.
[13:15:18] Script[gamemodes/bare.amx]: Run time error 3: "Stack/heap collision (insufficient stack size)"