Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Help With Recursion

Author
Message
SCI_CO
20
Years of Service
User Offline
Joined: 20th Apr 2004
Location: USA
Posted: 26th Jun 2004 15:35 Edited at: 26th Jun 2004 18:55
this was on UWTV for C Programing
it should loop


I don`t know whats rong
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 26th Jun 2004 18:36 Edited at: 26th Jun 2004 18:41
-On the first if, in the function, you should be checking for n=0. The reason is when n=1 the recursive call will be (n-1) which will be equal to 0. At that point the if condition will return 1.

-In the original call to the function you need to do something with the result. In the following example I chose to print the result.

-In DB, applications close if you do not have a loop or a wait key command.

try this:

rem **** Recursion example to Calculate Factorials

print fact(4)
wait key

function fact(number)
if number=0
result=1
else
result= number * fact(number-1)
endif
endfunction result


SCI_CO
20
Years of Service
User Offline
Joined: 20th Apr 2004
Location: USA
Posted: 26th Jun 2004 18:55
Ahaa I get it,
Thanks
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 26th Jun 2004 19:04
Glad that I could help.

Recursion is probably one of the least intuitive topics, that gets everyone at some point. But their solutions are elegant. Especially when used in combination with trees.

The elegance really shines when you first see a recursive code solution that can solve any maze. I have written one in C. If I have a chance I'll convert it over to DB. I couldn't imagine trying to write the solution without recursion, and search-depth left/right-first algorithms.

~zen


SCI_CO
20
Years of Service
User Offline
Joined: 20th Apr 2004
Location: USA
Posted: 27th Jun 2004 08:38
This is all new to me
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 28th Jun 2004 03:07
Im always happy when i find a use for recursion and successfully write it.

"eureka" - Archimedes

Login to post a reply

Server time is: 2024-11-26 23:37:18
Your offset time is: 2024-11-26 23:37:18