SRL Archive - General

loop in function itself, doesnt work?


Master BAW

2012-06-13 09:05:12

program test;

procedure a;
var
x: integer;
begin
x := random(10); //0 tot 9
Writeln(IntToStr(x) + ' ');

if(random(3) = 2) then a;

end;

procedure b;
var
x: integer;
begin
x := random(20)+10; //10 tot 19
writeln(IntToStr(x) + ' ');

if(random(3) = 2) then b;

end;

procedure c;
var
x: integer;
begin
x := random(30)+20; //20 tot 29
writeln(IntToStr(x) + ' ');

end;

procedure d;
begin
writeln('I''m Gone!');
end;

function ca: boolean;
begin
c();
if(random(100) <> 1) then ab
else
begin
d();
Result := True;
end;

end;

procedure bc;
begin
b();
ca();
end;

procedure ab;
begin
a();
bc;
end;








begin
ab;
end.


I made this script to test if I can make loops in the functions/procedures itself, but it doesn't work, and gives this error:

[Error] (43:28): Unknown identifier 'ab' at line 42

Why can't this work? How can I make this working, or do it differently with the same output?

Abu

2012-06-13 09:15:37

You have to call 'ab' above that function as Simba reads downwards.

riwu

2012-06-13 09:18:16

because u only have a procedure named "a", and another named "b", and NO procedure named "ab". If u want it to perform both "a" and "b" it should be
if(random(100) <> 1) then	
begin //add begin cos u r going to perform >1 actions
a; //1st action
b; //2nd action
end;
Also if u didnt create any parameter, u can just call the function without putting bracket (i.e. c; instead of c(); )

EDIT: ops didnt see u put ab behind it, u should always put all functions u want to use in a procedure above it.

Master BAW

2012-06-13 09:21:00

From: abu_jwka
You have to call 'ab' above that function as Simba reads downwards.


if you see my script u can see that that won't work because if i do that there is another function that is not indentified :) because it's a loop :P
but okay that sucks ,,,, in PHP it reads all the functions first without doing anything with them. :(

hmmmmmm how do i get this to work…

Kyle Undefined

2012-06-13 09:22:58

Something like this:

procedure ab; forward;

function ca: boolean;
begin
c();
if(random(100) <> 1) then ab
else
begin
d();
Result := True;
end;

end;

Master BAW

2012-06-13 09:31:44

From: Kyle Undefined
Something like this:

procedure ab; forward;

function ca: boolean;
begin
c();
if(random(100) <> 1) then ab
else
begin
d();
Result := True;
end;

end;



Thanks! This was EXACTLY what I was looking for! :)

Master BAW

2012-06-13 09:33:20

program test;

procedure a;
var
x: integer;
begin
x := random(10); //0 tot 9
Writeln(IntToStr(x) + ' ');

if(random(3) = 2) then a;

end;

procedure b;
var
x: integer;
begin
x := random(20)+10; //10 tot 19
writeln(IntToStr(x) + ' ');

if(random(3) = 2) then b;

end;

procedure c;
var
x: integer;
begin
x := random(30)+20; //20 tot 29
writeln(IntToStr(x) + ' ');

end;

procedure d;
begin
writeln('I''m Gone!');
end;

procedure ab; forward;

function ca: boolean;
begin
c();
if(random(100) <> 1) then ab
else
begin
d();
Result := True;
end;

end;

procedure bc;
begin
b();
ca();
end;

procedure ab;
begin
a();
bc;
end;








begin // ------------ main loop ---------- \\
ab;
end.

This program totally works :) It's a loop without a while, for or repeat :) This has unlimited functionalities :) Try it yourself. With this you can make very advanced loops :)

Brandon

2012-06-13 09:38:33

From: MasterCrimez
This program totally works :) It's a loop without a while, for or repeat :) This has unlimited functionalities :) Try it yourself. With this you can make very advanced loops :)



Until you run out of memory from an infinite/large recursive call. Well at least in other languages. Not sure about pascal script but it should essentially be the same.

http://en.wikipedia.org/wiki/Stack_overflow#Very_deep_or_infinite_recursion


Each function calling eachother like that over and over is bound to be exactly similar to:


Function Main: Boolean;
begin
Result:= Main;
end;

Which just so happens to run my CPU and memory usage all the way up when ran too long.

Master BAW

2012-06-13 09:48:19

From: Brandon
Until you run out of memory from an infinite/large recursive call. Well at least in other languages. Not sure about pascal script but it should essentially be the same.

http://en.wikipedia.org/wiki/Stack_overflow#Very_deep_or_infinite_recursion


Each function calling eachother like that over and over is bound to be exactly similar to:


Function Main: Boolean;
begin
Result:= Main;
end;

Which just so happens to run my CPU and memory usage all the way up when ran too long.


I understand, but that's what a bot is … it runs forever… it is the same as this

Program test
function main: boolean;
begin end;

begin
repeat main until 1=2;
end;

but I think it isn't very hard for the computer if you use this:



Function Main: Boolean;
begin
wait(500);
Result:= Main;
end;

Is it?

Anyway, I'm finding this very interesting, because you can make very advanced loops between functions with this. I hope some1 can explain if this will work fine with pascal, or NOT.

Thanks!

Brandon

2012-06-13 09:50:31

I don't know if it will work fine with pascal or not but..


Function Main: Boolean;
begin
end;


begin
Repeat
Main;
Until(False);
end.

Would not be the same as the main function returns a value and when a function returns, all cleanup is done and the stack is cleared. But like I said, I don't know for pascal.

Master BAW

2012-06-13 09:53:53

From: Brandon
I don't know if it will work fine with pascal or not but..


Function Main: Boolean;
begin
end;


begin
Repeat
Main;
Until(False);
end.

Would not be the same as the main function returns a value and when a function returns, all cleanup is done and the stack is cleared. But like I said, I don't know for pascal.


I don't know what you mean by cleanup and stack clearing, but I'll read
http://en.wikipedia.org/wiki/Stack_overflow#Very_deep_or_infinite_recursion
now because maybe I understand it then ;) anyway, thanks for the explenation.

Master BAW

2012-06-13 10:07:31

found this tutorial: reading now:

http://villavu.com/forum/showthread.php?t=80626

Brandon

2012-06-13 10:14:06

From: MasterCrimez
found this tutorial: reading now:

http://villavu.com/forum/showthread.php?t=80626



That is a good example tutorial on recursive functions. I just couldn't explain it properly because my brain thinks to messed up and I wasn't sure that it happens in pascal as it does not happen in languages that force tail recursion optimization which you'll eventually learn if you follow up on this.

Anyway I'll look into whether base recursion in pascal will overflow..

Essentially base recursion is a function that calls another function which in turn calls the caller.. Example would be what you have vs. what I said would be similar.


Edit: Found out both are the exact same recursive functions. One is just indirect whereas the other is direct recursion and thus both will cause the same overflow if used excessively before returning.