Basic Recursion Problem

This is a discussion on Basic Recursion Problem within the C++ Programming forums, part of the General Programming Boards category; I have a problem using the recursion. what is the difference between these 2 codes? Code: void test(int i) { ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    16

    Basic Recursion Problem

    I have a problem using the recursion.
    what is the difference between these 2 codes?

    Code:
    void test(int i)
    {
        if(i==5) return;
        test(i+1);
        cout<<"print\n";//why this part is accessed????
        
    }


    suppose we have called it by test(1);
    in the above code why the cout<<"print\n" portion is accessed when i execute the progeam.
    i understand what recursion is.
    i believe this is called tail recursion.but i cant understand its behaviour.
    can you give me an idea on this?
    please explain the its behaviour.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    i believe this is called tail recursion.
    Tail call == the recursive call is the last thing in the function.
    Your function: the last thing is the cout statement.
    So obviously the recursive call is not tail recursive.

    By the way, tail call optimization is an optimization, and therefore MUST NOT change the program behavior. Not executing the cout would quite obviously be a change in the behavior.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You understand I suppose that if you see the cout or not depends on the argument of the function.

    Run this simple code, I think it reveals much of the behavior. This is caused because the recursion unwinds.
    Code:
    #include <iostream>
    using namespace std;
    
    void test(int i)
    {
        if(i==5) return;
        test(i+1);
       cout<<print <<i<<endl;
         
    }
    int main () {
        test(5);
        return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic recursion question
    By benrogers in forum C Programming
    Replies: 8
    Last Post: 02-28-2011, 02:23 PM
  2. Replies: 5
    Last Post: 01-13-2011, 11:12 AM
  3. recursion problem
    By claudiat13 in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2010, 10:26 PM
  4. Recursion problem
    By ramayana in forum C Programming
    Replies: 3
    Last Post: 11-13-2006, 10:54 PM
  5. basic recursion function question. typo in book?
    By navyzack in forum C Programming
    Replies: 6
    Last Post: 04-18-2005, 11:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21