Here\'s my code
using System;
public class Program
{
public static void Method(int flowerInVase)
{
if (flowerInVase > 0)
{
The structure of the calls look like this. Maybe this visualization will help you understand why the numbers print 1, 2, 3 and not 3, 2, 1:
Method(3);
flowerInVase > 0 ?
Yes - Method(2);
flowerInVase > 0 ?
Yes - Method(1);
flowerInVase > 0 ?
Yes - Method(0);
flowerInVase > 0 ?
No
WriteLine(1);
WriteLine(2);
WriteLine(3);
The reason is because your call to Method(flowerInVase - 1)
comes before the call to WriteLine(flowerInVase)
. Execution jumps to a different copy of the method before it has a chance to print the number it is on.
You're getting 1,2,3 because of the way the lines in your if
statement are ordered.
Main()
calls Method(3)
.
Method(3)
calls Method(2)
before it has a chance to print "3". Execution immediately jumps to the top of Method
; your first call to Method
, with flowersinvase=3
, won't complete until the recursive call does. Likewise, Method(2)
immediately calls Method(1)
, and Method(1)
calls Method(0)
.
Method(0)
does nothing and returns to Method(1)
, exactly where it left off; the next line is your WriteLine
call, which prints "1" and then returns, which picks up the call to Method(2)
where it left off, printing "2", and so on for "3".
You would only get "3,2,1" if the methods you called ran to completion before jumping to any methods they called recursively, which is not how C# works. The thing you have to remember about method calls is that once you call a method, execution jumps immediately to the start of the method you called; the code after the method call will not execute until the method returns.
The compiler uses writeline only when it's done doing recursion.
It does that because you told it to do that. In your code, first perform the recursion (call Method()
) and only when that finishes, you write the number.
If you want to write the number first, before performing recursion, you need to switch of statements in your code:
public static void Method(int flowerInVase)
{
if (flowerInVase > 0)
{
Console.WriteLine(flowerInVase);
Method(flowerInVase - 1);
}
}
Because it does this:
flowerInVase = 3
call Method(3)
call Method(2)
call Method(1)
WriteLine(1)
WriteLine(2)
WriteLine(3)
Output then is:
1
2
3
If you reverse the lines:
Console.WriteLine(flowerInVase);
Method(flowerInVase - 1);
It will print first, then recurse, so it will print 3, 2, 1 instead.
Why the console.writeline works only when stuck pops up, why it ain't writing the numbers on the way methods go to the termination, like 3,2,1?
Because your Method(flowerInVase - 1);
is called before Console.WriteLine(flowerInVase);
.
If you follow the code execution line by line this makes sense. If you break it down what it really is saying is you always create a new instance of the recursive call before you ever get a chance to write anything to the console. Only once you have created all the calls you are allowed to (your if-statement fails) is the control of the thread returned to the existing methods.
Create method -> check parameter
-> create method - > check parameter
-> create method -> check parameter
etc. etc.
-> print value
-> print value
-> print value
So now that you see it is returning control back up the way it was called. The last called function needs to complete its operation before the one that called it can complete.