Results 1 to 7 of 7

Thread: recursion

  1. #1

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    recursion

    What is it? What is it used for? How do I use it? What's godd with it? What is not so good with it?
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Recursion is a function that calls itself. If recursion is absolutely guaranteed to be a few calls (referred to as depth of recursion)
    then it has very little impact on the system.

    Otherwise, as a general solution, it's a bad idea. Every recursive call pushes all of the variables and registers of the function onto the stack as they currently exist. If you call a function recursively enough you run out of stack space (memory). Basically it results in bad performance for most applications.

    The other issue: a lot of recursive routines are impossible to read,
    so that nobody, even yourself can work with them easily six months later.

    The main use for recursion is in parser routines that use setjmp to return from parsing errors.

  3. #3

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ok, so recursion is nothing important for a newbie to learn?
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  4. #4
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    It's not all that hard - just learn to avoid it mostly.

  5. #5

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ok, I got this book that asks me to write a function that gets a Fibonacci number. The function should use recursion to get the number and should look like this:
    Code:
    long fib(int n);
    If I pass it 20 it should return the twentieth Fibonacci number. How would I do this? Please explain in great detail because I find this hard to understand.

    Fibonacci numbers are the numbers 1,2,3,5,8,13,21... To get a number you add the two before it so 1+2=3 2+3=5 3+5=8...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  6. #6
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking how to do it

    Ok, i'm just making this up as i go along, you would have to try it first, fix any errors (there are always some )

    //Global variables:
    long Num_A=1;
    long Num_B=1;
    long Num_C=0;

    long fib(int n){
    if (n<3)
    {return Num_B;}
    Num_C=Num_B;
    Num_B+=Num_A;
    Num_A=Num_C;
    return fib(n-1);
    }

    Ok, this is just a possible way to do it, i'm not exactly sure if it works. Basically, you have a function that adds two number, and call this function (n-2) times. n-2 because you start with 2 numbers already.
    However, this may not work, the global variable stuff may stuff up, like not having the Byval call in VB
    sql_lall

  7. #7

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Thank you, I'll try this when I get home
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width