Module: subroutines. recursion


Problem

2/8

Recursion. Cycle Simulation

Theory Click to read/hide

We have seen that recursion is the repeated execution of contained instructions 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 operation of the for loop. 
The for loop contains a step counter variable. In a recursive subroutine, such a variable can be passed as a parameter.

//LoopImitation() procedure with two parameters
//First parameter – step counter, second parameter – total number of steps
procedure LoopImitation(i, n: integer);
begin
    writeln('Hello N ', i); // Operator to be repeated for any value of i
    if i < n then //Until the loop counter becomes equal to the value n,
        LoopImitation(i + 1, n); //call a new instance of the procedure, with the parameter i+1 (transition to the next value i)
end; 

Problem

Study the program below and issue a procedure call with parameters i=1, n=10 in the main program
//LoopImitation() procedure with two parameters
//First parameter – step counter, second parameter – total number of steps
procedure LoopImitation(i, n: integer);
begin
    writeln('Hello N ', i); // Operator to be repeated for any value of i
    if i < n then //Until the loop counter becomes equal to the value n,
        LoopImitation(i + 1, n); //call a new instance of the procedure, with the parameter i+1 (transition to the next value i)
end;

begin
    // here it is necessary to issue a procedure call with parameters i=1, n=10
end.