Module: subroutines. recursion


Problem

2/12

Recursion. Cycle Simulation

Theory Click to read/hide

We found out that recursion is the repeated execution of contained commands in a subroutine. And this, in turn, is similar to the work of the cycle. There are programming languages ​​in which the loop construct is absent at all, for example, Prolog. 
Let's try to simulate the work of the loop for
The for loop contains a step counter variable. In a recursive subroutine, such a variable can be passed as a parameter.

// procedure LoopImitation() with two parameters
// first parameter – step counter, second parameter – total number of steps
static void LoopImitation(int i, int n)
{
  Console.WriteLine("Hello N" + i); // statement to be repeated for any value i 
  if (i < n) // until loop counter equals n,
  {
    LoopImitation(i+1, n); // calling a new instance procedure, with parameter i+1 (go to the next i value)
  }
} 

Problem

Examine the program below and set up a procedure call in the main program with parameters i = 1 , n = 10.