Basic Recursion

AlHal2
AlHal2 used Ask the Experts™
on
I choose to print numbers 1 to 5 in this code.  When I step through it using F11 I get to the line
return stval;

Open in new window

It goes to the end of the routine, but does not go back to the calling command  
printNatural(1, ctr);

Open in new window

Instead it goes back to  this line of code
return printNatural(stval + 1, ctr);

Open in new window

several time before it goes back to the static main method.
Why does it not go straight back to static main after
return stval;

Open in new window


using System;

namespace ConsoleApp10
{

    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("\n\n Recursion : Print the first n natural number :\n");
            Console.Write("---------------------------------------------------\n");
            Console.Write(" How many numbers to print : ");
            int ctr = Convert.ToInt32(Console.ReadLine());
            // Call recursive method with two parameters.	
            printNatural(1, ctr);
            Console.Write("\n\n");

        }
        static int printNatural(int stval, int ctr)
        {
            if (ctr < 1)
            {
                return stval;
            }
            ctr--;
            Console.Write(" {0} ", stval);
            return printNatural(stval + 1, ctr);
        }
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Because your control variable has to be less than 1.  Until your control variable is less than one, your function will call itself (creating a new instance) over and over while deducting 1 from the control variable.  Each created instance get's placed on top of the call stack.  The first time you get to the line:
return stval;

Open in new window

You have 6 instances of your function on the stack:Capture.PNGAs each instance is popped from the stack, it's result is sent to the instance which initiated the call, the last instance is the only instance which will return to the main method call:Capture.PNGCapture.PNGUsing the call stack window you can see all of this happening.

-saige-

Author

Commented:
What would be a better implementation?

Commented:
Well in this instance, you want to simply print out numbers in sequence so a *better* implementation would do just that; e.g. -
using System;

namespace EE_Q29139617
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("\n\n Looping : Print the first n natural number :\n");
            Console.Write("---------------------------------------------------\n");
            Console.Write(" How many numbers to print : ");
            int ctr = Convert.ToInt32(Console.ReadLine());
            // Call recursive method with two parameters.	
            printNatural(ctr);
            Console.Write("\n\n");
            Console.ReadLine();
        }

        static int printNatural(int ctr)
        {
            for (int i = 1; i < ctr + 1; i++)
            {
                Console.Write(" {0} ", i);
            }
            return 0;
        }
    }
}

Open in new window

Understand that this is not a recursive implementation.  It is a looping implementation.

-saige-
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Author

Commented:
Thanks.
AndyAinscowFreelance programmer / Consultant

Commented:
If you want to learn about recursion then:
        static void Main(string[] args)
        {
            Console.Write("\n\n Recursion : Print the first n natural number :\n");
            Console.Write("---------------------------------------------------\n");
            Console.Write(" How many numbers to print : ");
            int ctr = Convert.ToInt32(Console.ReadLine());
            // Call recursive method with two parameters.	
            printNatural(ctr);
            Console.Write("\n\n");
            Console.Read();
        }
        static int printNatural(int ctr)
        {
            if (ctr < 1)
            {
                return ctr;
            }
            Console.Write(" {0} ", ctr);
            ctr--;
            return printNatural(ctr);
        }

Open in new window


which gives
SNAG-0033.bmp

Author

Commented:

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial