Rastreamento da função fatorial recursiva function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end Supondo-se o seguinte trecho de programa... : A := 4; F := Fat(A);
function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; Alocação de memória N 4
function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; Alocação de memória N 4
function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; Alocação de memória N 4 4 * Fat(3) Nova ativação de Fat
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end;
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end;
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2) Nova ativação de Fat
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2) N 2 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end;
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2) N 2 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end;
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2) N 2 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 2 * Fat(1) Nova ativação de Fat
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2) N 2 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 2 * Fat(1) N 1 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end;
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2) N 2 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 2 * Fat(1) N 1 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end;
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2) N 2 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 2 * Fat(1) N 1 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 1 * Fat(0) Nova ativação de Fat
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2) N 2 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 2 * Fat(1) N 1 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 1 * Fat(0) N 0 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end;
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2) N 2 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 2 * Fat(1) N 1 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 1 * Fat(0) N 0 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; Função encerra e retorna 1 Fat(0) = 1
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2) N 2 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 2 * Fat(1) N 1 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 1 * Fat(0)
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2) N 2 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 2 * Fat(1) N 1 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 1 * 1 = 1 Função encerra e retorna 1 Fat(1) = 1
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2) N 2 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 2 * Fat(1)
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2) N 2 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 2 * 1 = 2 Função encerra e retorna 2 Fat(2) = 2
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * Fat(2)
Alocação de memória N 4 N 3 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3) function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 3 * 2 = 6 Função encerra e retorna 6 Fat(3) = 6
Alocação de memória N 4 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * Fat(3)
Alocação de memória N 4 function Fat (N: byte): Longint; begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1) end; 4 * 6 = 24 Função encerra e retorna 24 Fat(4) = 24
: A := 4; F := Fat(A); : Alocação de memória A 4 F 24 Retorno ao módulo que fez a 1a. chamada...