Use a recursive method, travel, to journey from start to finish : Function Definition « Language Basics « C# / C Sharp






Use a recursive method, travel, to journey from start to finish

Use a recursive method, travel, to journey from start to finish
using System;

public class Journey {

  private static String indent = "";
  
  public static void TakeOneStep(int step) {
    Console.WriteLine("{0}Taking step {1}", indent, step);
  }
  
  public static void Move(int start, int finish) {
    string oldIndent = indent;
    Console.WriteLine("{0}Starting move from {1} to {2}", indent, start, finish);
    if (start < finish) {
      TakeOneStep(start);
      indent += "   "; 
      Move(start+1, finish);
      indent = oldIndent;
    }
    Console.WriteLine("{0}Finishing move from {1} to {2}",indent, start, finish);
  }
  public static void Main(String [] args) {
    Move(1, 10);
  }
} 


           
       








Related examples in the same category

1.Recursive sum methodRecursive sum method
2.Recursive Factorial method.
3.Recursive function in actionRecursive function in action
4.Define functionDefine function
5.Catch StackOverflowException for recursive function