Pending Method Instances of Different Methods


To understand how recursion works, it is important to first understand the mechanics of chains of method calls. Listing 23.1 does not contain any recursive method calls and does not solve any particular problem, it merely demonstrates what happens when one method calls a second method that calls a third method that calls a fourth method.

Listing 23.1 MethodCallDemo.cs
01: using System; 02: 03: class MethodCallDemo 04: { 05:     public static void Main() 06:     { 07:         MethodA(3); 08:     } 09: 10:     public static void MethodA(int level) 11:     { 12:         Console.WriteLine("First part of MethodA. Level: { 0} ", level); 13:         MethodB(level - 1); 14:         Console.WriteLine("Last part of MethodA.  Level: { 0} ", level); 15:         return; 16:     } 17: 18:     public static void MethodB(int level) 19:     { 20:         Console.WriteLine("First part of MethodB. Level: { 0} ", level); 21:         MethodC(level - 1); 22:         Console.WriteLine("Last part of MethodB.  Level: { 0} ", level); 23:         return; 24:     } 25: 26:     public static void MethodC(int level) 27:     { 28:         Console.WriteLine("              MethodC. Level: { 0} ", level); 29:         return; 30:     } 31: } First part of MethodA. Level: 3 First part of MethodB. Level: 2               MethodC. Level: 1 Last part of MethodB.  Level: 2 Last part of MethodA.  Level: 3 

Apart from the Main method, Listing 23.1 contains three similar methods called MethodA, MethodB, and MethodC. Each method accepts an argument of type int that is assigned to a formal parameter called level. Furthermore, the methods contain WriteLine calls to help us track the flow of execution in the program. Finally, each method (excluding MethodC) contains a call to another method. This call is positioned between the two WriteLine statements and provides an argument that is equal to level minus 1.

As illustrated in Figure 23.1 the Main method starts a chain of method calls by calling MethodA in line 7 with the argument 3 that is assigned to MethodA's parameter called level. The flow of execution then moves through line 12 that contains a call to WriteLine. MethodB then is called in line 13 with the argument 2 (level-1) . It is important to notice that even though execution at this point continues at MethodB, MethodA is still waiting to have its last part executed; only when execution reaches MethodA's return statement will MethodA terminate. In the meantime, while the flow of execution is at another method, MethodA (containing its local variable level with the value 3) is considered to be a pending method instance, kept in memory for use when flow of execution returns back to this method. The runtime makes sure that all local variables and formal parameters for this method are stored until the method is properly finished. In this case, the parameter level with the value 3 is stored in memory.

Figure 23.1. Four method instances.

During the execution of MethodB, a call is made to MethodC. While MethodC is being executed, the program contains three pending method instances (see Figure 23.1) one of Main, one of MethodA, and one of MethodB. Notice that MethodA's local variable level has the value 3, and MethodB's local variable level has the value 2, while MethodC's level has the value 1.

MethodC does not call any other methods but, instead, simply returns. This causes execution to re-enter MethodB, which continues from where the call to MethodC was made and, thus, has its last part executed. The same happens when MethodB returns execution to MethodA. Finally, execution returns to Main that only contains the one method call to MethodA, so the program ends.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: