rastreamento da função fatorial recursiva function fat (n: byte): longint; begin if n = 0 then fat...

24
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);

Upload: mohammed-sole

Post on 30-Mar-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

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);

Page 2: 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

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

Page 3: 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

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

Page 4: 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

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

Page 5: 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

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;

Page 6: 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

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;

Page 7: 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

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

Page 8: 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

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;

Page 9: 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

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;

Page 10: 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

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

Page 11: 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

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;

Page 12: 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

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;

Page 13: 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

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

Page 14: 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

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;

Page 15: 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

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

Page 16: 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

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)

Page 17: 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

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

Page 18: 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

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)

Page 19: 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

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

Page 20: 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

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)

Page 21: 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

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

Page 22: 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

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)

Page 23: 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

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

Page 24: 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

: A := 4;F := Fat(A); :

Alocação de memória

A4

F24

Retorno ao módulo que fez a 1a. chamada...