A function that calls itself
- Recursion is when a function calls itself.
- Each call should work on a smaller version of the problem.
- Done right, the problem shrinks until it is trivial to solve.
Fungsi yang memanggil dirinya sendiri
- Resursivitas adalah ketika sebuah fungsi memanggil dirinya sendiri.
- Setiap pemanggilan harus bekerja pada versi masalah yang lebih kecil.
- Dilakukan dengan benar, masalah mengecil hingga mudah diselesaikan.
Base case and recursive case
- The base case is the simplest case — it stops the recursion.
- The recursive case calls the function again on a smaller input.
- Without a base case the function never stops (an error).
Kasus dasar dan kasus rekursif
- Kasus dasar adalah kasus paling sederhana — ini menghentikan resursivitas.
- Kasus rekursif memanggil fungsi lagi pada input yang lebih kecil.
- Tanpa kasus dasar, fungsi tidak akan pernah berhenti (error).
def countdown(n):
if n == 0:
print("Go!")
return
print(n)
countdown(n - 1)
countdown(3)
A worked example: factorial
- The factorial
n!meansn × (n-1) × ... × 1. - In recursive form:
n! = n × (n-1)!, and the base case is0! = 1. - Each call multiplies
nby the factorial of one less.
Contoh terpecahkan: faktorial
- Faktorial
n!berartin × (n-1) × ... × 1. - Dalam bentuk rekursif:
n! = n × (n-1)!, dan kasus dasarnya adalah0! = 1. - Setiap pemanggilan mengalikan
ndengan faktorial dari satu kurang.
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(4))
How the computer runs it
- Each call is paused on a call stack while it waits for the inner call.
- When the base case returns, the paused calls finish one by one.
- Too many calls overflow the stack — Python raises a
RecursionError.
Bagaimana komputer menjalankannya
- Setiap pemanggilan ditahan di stack pemanggilan sementara menunggu pemanggilan internal.
- Ketika kasus dasar mengembalikan nilai, pemanggilan yang tertahan selesai satu per satu.
- Terlalu banyak pemanggilan menyebabkan overflow stack — Python raises a
RecursionError.
In Cambridge pseudocode
- A recursive function names its base case and recursive case clearly.
Dalam pseudocode Cambridge
- Fungsi rekursif menamakan kasus dasar dan kasus rekursifnya dengan jelas.
FUNCTION Factorial(n : INTEGER) RETURNS INTEGER
IF n = 0 THEN
RETURN 1 // base case
ELSE
RETURN n * Factorial(n - 1) // recursive case
ENDIF
ENDFUNCTION
Common mistakes
- Every recursion needs a base case, or it overflows the call stack.
- Each call must move closer to the base case.
Kesalahan umum
- Setiap resursivitas membutuhkan kasus dasar, atau akan menyebabkan overflow stack pemanggilan.
- Setiap pemanggilan harus bergerak lebih dekat ke kasus dasar.
Now you try
- Give each function a base case and a recursive case.
- Press Check answer to test your code.
Sekarang Anda coba
- Berikan setiap fungsi kasus dasar dan kasus rekursif.
- Tekan Periksa jawaban untuk menguji kode Anda.
Recursion returns from the leaves · Recursion mengembalikan dari daun-daunnya
Each call splits into smaller calls; answers return up from the base cases. · Setiap panggilan terpecah menjadi panggilan yang lebih kecil; jawaban kembali naik dari kasus dasar.
Write a recursive sum_to(n) that returns 1 + 2 + ... + n. The base case is sum_to(0) which is 0. · Tulis fungsi rekursif sum_to(n) yang mengembalikan 1 + 2 + ... + n. Kasus dasar adalah sum_to(0) yang merupakan 0.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Write a recursive power(base, exp) that returns base raised to exp. The base case is exp == 0, which gives 1. · Tulis fungsi rekursif power(base, exp) yang mengembalikan base dipangkatkan exp. Kasus dasar adalah exp == 0, yang menghasilkan 1.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Write a recursive count_up(n) that returns the list [1, 2, ..., n]. The base case is count_up(0) which is the empty list []. · Tulis fungsi rekursif count_up(n) yang返回列表 [1, 2, ..., n]. Kasus dasarnya adalah count_up(0), yaitu daftar kosong [].
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.