rastreamento da função fatorial recursiva function fat (n: byte): longint; begin if n = 0 then fat...
TRANSCRIPT
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
N4
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
Alocação de memória
N4
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
Alocação de memória
N4
4 * Fat(3)
Nova ativação de Fat
Alocação de memória
N4
N3
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
N4
N3
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
N4
N3
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
N4
N3
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)N2
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
Alocação de memória
N4
N3
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)N2
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
Alocação de memória
N4
N3
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)N2
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
N4
N3
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)N2
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
2 * Fat(1)
N1
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
Alocação de memória
N4
N3
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)N2
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
2 * Fat(1)
N1
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
Alocação de memória
N4
N3
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)N2
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
2 * Fat(1)
N1
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
N4
N3
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)N2
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
2 * Fat(1)
N1
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
1 * Fat(0)
N0function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
Alocação de memória
N4
N3
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)N2
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
2 * Fat(1)
N1
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
1 * Fat(0)
N0function 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
N4
N3
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)N2
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
2 * Fat(1)
N1
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
N4
N3
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)N2
function Fat (N: byte): Longint;begin if N = 0 then Fat := 1 else Fat := N * Fat (N-1)end;
2 * Fat(1)
N1
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
N4
N3
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)N2
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
N4
N3
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)N2
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
N4
N3
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
N4
N3
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
N4
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
N4
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
A4
F24
Retorno ao módulo que fez a 1a. chamada...