SRL Archive - OSR Help

Having a func or proc call itself.


xdarkshadowx

2013-01-05 03:17:30

Is it possible to have a function or procedure call itself? If not I can just use a loop, but i get an access violation having my function call itself. Just wondering if there's a workaround or something. I recall reading about having a function call itself as a form of loop but forget if it works with simba.

Thanks :)

DannyRS

2013-01-05 03:18:41

Have you tried using


Continue;

I believe that starts the current Begin/End loop again, works for everything I've had to do

Sex

2013-01-05 03:40:56

What you're talking about is recursion. If you write your function correctly it is very possible. You could also try the loop and use the continue keyword to go to the top and the break keyword to break out of it.

Le Jingle

2013-01-05 03:41:57

are you using proper recursive method implementation?

ex. (from web - translated to ps)

program Factorial;

function fact(n: integer): string;
begin
if (n = 1) then
begin
result := '1';
exit;
end else
begin
result := IntToStr(n) + ' * ' + (fact(n - 1));
exit;
end;
end;

begin
writeln(fact(10));
end.

xdarkshadowx

2013-01-05 03:42:30

From: DannyRS
Have you tried using


Continue;

I believe that starts the current Begin/End loop again, works for everything I've had to do


how would that work inside an If statement?

here's the code trying to call itself and failing: what i've tried so far lol.
Function SelectFromMiningOverView(x: integer): boolean; 
begin
if((findPassiveTargets.x = x) or (findPassiveTargets.y = x) or (findActiveTarget = x)) then
begin
SelectFromMiningOverView(x+1);
end else
begin
SelectFromOverView(x);
end;
end;

I know I could just put the if statement inside a loop and have Exit; right after it gets into the second part of the If. But I try to mix things up and learn new things. You can just yell at me if i'm being stupid though.

riwu

2013-01-05 04:00:22

Using a loop is much simpler and safer than making a recursive function, in fact i don't think recursive functions should be used in PS due to some memory bugs?


procedure SelectFromMiningOverView(x: integer);
begin
while True do
if((findPassiveTargets.x = x) or (findPassiveTargets.y = x) or (findActiveTarget = x)) then
Inc(x)
else
begin
SelectFromOverView(x);
Exit;
end;
end;

xdarkshadowx

2013-01-05 04:03:42

From: riwu
Using a loop is much simpler and safer than making a recursive function, in fact i don't think recursive functions should be used in PS due to some memory bugs?


procedure SelectFromMiningOverView(x: integer);
begin
while True do
if((findPassiveTargets.x = x) or (findPassiveTargets.y = x) or (findActiveTarget = x)) then
Inc(x)
else
begin
SelectFromOverView(x);
Exit;
end;
end;


Yeah, I was trying to mess around with it just now but it seems my Access violation error is due to this line:
if((findPassiveTargets.x = x) or (findPassiveTargets.y = x) or (findActiveTarget = x)) then

no clue why that's invalid though. hm.