just recustion
Author |
Message |
Unreal_Origin
|
Posted: Tue Nov 02, 2004 4:02 pm Post subject: just recustion |
|
|
the is recusion it is in my paint program
code: |
var mx, my, mb : int
var originalcolour : int
var thecolour : int := 4
proc fillingin (x, y, originalcolour, thecolour : int)
drawdot (x, y, thecolour)
if whatdotcolor (x + 1, y) = originalcolour then
fillingin (x + 1, y, originalcolour, thecolour)
end if
if whatdotcolor (x, y + 1) = originalcolour then
fillingin (x, y + 1, originalcolour, thecolour)
end if
if whatdotcolor (x, y - 1) = originalcolour then
fillingin (x, y - 1, originalcolour, thecolour)
end if
if whatdotcolor (x-1, y ) = originalcolour then
fillingin (x-1, y , originalcolour, thecolour)
end if
end fillingin
drawoval (255, 255,100, 100, 7)
drawbox (0,0,maxx,maxy,7)
loop
mousewhere (mx, my, mb)
if mb = 1 then
originalcolour := whatdotcolor (mx, my)
fillingin (mx, my, originalcolour, thecolour)
end if
end loop
|
here is the problem i think it is with turing though, it is that it "stake overflow" but w/e (note this happens when you click to fast before it is done) |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Tue Nov 02, 2004 6:55 pm Post subject: (No subject) |
|
|
The key to recursion is providing a way for your function/procedure to escape the recursion. At least one branch of that if...elsif...else statement has to do something other than call the function/procedure. |
|
|
|
|
|
Andy
|
Posted: Wed Nov 03, 2004 5:07 pm Post subject: (No subject) |
|
|
nah wtd.. it works.. when it finishes drawing, it'll stop cuz every where it checks will return the right color |
|
|
|
|
|
wtd
|
Posted: Wed Nov 03, 2004 7:58 pm Post subject: (No subject) |
|
|
No, it really doesn't. It just keeps running and running, until it overflows the stack. Try inserting some code after the code originally posted. The circle will get drawn, and then nothing else will happen, because that procedure is still running.
Every recursive function or procedure has to have a way out.
Consider a basic (very quickly written) factorial function:
code: | function factorial (n : int) : int
if n <= 1 then
result 1
else
result n * factorial (n - 1)
end if
end factorial
put factorial (4) |
You'll notice that there's a very clear exit point. If "n" is less than or equal to 1, the function returns 1. Eventually, it'll get to this point and the function won't call itself. |
|
|
|
|
|
zylum
|
Posted: Wed Nov 03, 2004 9:15 pm Post subject: (No subject) |
|
|
if you read his code you can see that the procedure does termiate. the function will not be called every time since the four if statements will not be satisfied everytime. and since it's a procedure, it does not need ot return a value. to prove this, exit the final loop after the function call in the if statement which checks for mousedown:
code: | var mx, my, mb : int
var originalcolour : int
var thecolour : int := 4
proc fillingin (x, y, originalcolour, thecolour : int)
drawdot (x, y, thecolour)
if whatdotcolor (x + 1, y) = originalcolour then
fillingin (x + 1, y, originalcolour, thecolour)
end if
if whatdotcolor (x, y + 1) = originalcolour then
fillingin (x, y + 1, originalcolour, thecolour)
end if
if whatdotcolor (x, y - 1) = originalcolour then
fillingin (x, y - 1, originalcolour, thecolour)
end if
if whatdotcolor (x - 1, y) = originalcolour then
fillingin (x - 1, y, originalcolour, thecolour)
end if
end fillingin
drawoval (255, 255, 100, 100, 7)
drawbox (0, 0, maxx, maxy, 7)
loop
mousewhere (mx, my, mb)
if mb = 1 then
originalcolour := whatdotcolor (mx, my)
fillingin (mx, my, originalcolour, thecolour)
exit
end if
end loop
put "it terminates" |
|
|
|
|
|
|
bugzpodder
|
Posted: Wed Nov 03, 2004 9:34 pm Post subject: (No subject) |
|
|
your function is fine as long as originalcolour and thecolour are different. when they are the same, you'll get into an infinite loop and cause stack overflow |
|
|
|
|
|
wtd
|
Posted: Wed Nov 03, 2004 9:43 pm Post subject: (No subject) |
|
|
Ah. His use of several if statements so close together made my brain think of a chain of if...elsif...else. I was under the impression at least one of those eventualities would happen. |
|
|
|
|
|
Andy
|
Posted: Thu Nov 04, 2004 6:11 pm Post subject: (No subject) |
|
|
lol its ok wtd.. we still respect you lol even if u suck :p jk |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|