Find the depth of recursion in this program

program with_rec_func; {$APPTYPE CONSOLE} uses SysUtils; var n:integer; function X(i:integer):integer; begin if i<3 then X:=-3 else X:=X(i-1)+X(i-2)+4; X:= Result; end; begin n:=0; repeat inc(n); until X(n)>0; writeln(' Least element with a plus sign X(n) = ',X(n)); writeln(' Its number n = ',n); readln; end. 

Closed due to the fact that the essence of the question is not clear to the participants of Igor , Kromster , freim , Yaant , Vadizar on 24 April at 15:23 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    Draw a call tree on paper, you will see how high it reaches depending on n.

    Then check by adding the level parameter to the function, pass it increased by 1 for each recursive call, output at the moment the recursion stops.

    And remove it: X:= Result;

    • I don’t understand how to draw a tree specifically for this program, n changes in ascending order and this is misleading for me, if I’d draw the other way around - Alice97
    • The function argument does not increase. - MBo
    • I just look at the work of the stack, for me it is not clear what I'm coming from, I don’t have any idea how to draw a tree to this particular program - Alice97
    • Take, for example, the function with argument 6 and draw two branches recurs. calls - with argument 5 and with argument 4, for each branch you continue the same. - MBo 6:19 pm
    • Yes, I understand that, but it’s not clear to me here, first x (1) is called, then x (2), x (3) from which 2 branches x (2) and x (1) already go, then x ( 4), from which - x (3), from which x (2) and x (1), and x (2), in turn, then x (5) and the same there, they are not called sequentially, as it would be if x (5) were immediately called, where everything is clear all the branches one after another, and how to be in this program, they are supposed to be located separately in more than one tree, sorry, maybe I misunderstand something)) - Alice97 pm