🔄 Cap 17: Recursividade: Caso Base, Passo Recursivo, Call Stack e Otimizações - Exemplos Práticos
Código executável completo demonstrando os conceitos abordados em Go.
🗺️ Fluxo de Execução
flowchart TD
REC["fatorial(4)"] --> R3["4 * fatorial(3)"]
R3 --> R2["3 * fatorial(2)"]
R2 --> R1["2 * fatorial(1)"]
R1 --> BASE["Caso Base: fatorial(1) = 1"]
style REC fill:#e1f5fe,stroke:#03a9f4,stroke-width:1px
style BASE fill:#e8f5e9,stroke:#4caf50,stroke-width:2px
📄 Código de Demonstração (recursao.go)
package main
import "fmt"
func fatorial(n int) int {
\if n <= 1 {
\return 1
}
\return n * fatorial(n-1)
}
func main() {
\fmt.Printf("Fatorial de 5: %d\n", fatorial(5))
}
🚀 Saída Esperada no Terminal
$ go run recursao.go
Fatorial de 5: 120
🧭 Navegação Rápida
| 📖 Teoria | 📊 Slides | 🧠 Quiz | 💻 Exemplos | 🧩 Exercícios | | :— | :— | :— | :— | :— | | Ler Tópico | Ver Slides | Fazer Quiz | Ver Código | Praticar |