Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wpforms-lite domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/ragipunal.com/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the businext domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/ragipunal.com/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wordpress-seo domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/ragipunal.com/wp-includes/functions.php on line 6114
Python Özyineli Fonksiyonlar - Recursive Function - RAGIP ÜNAL

Blog

Python Özyineli Fonksiyonlar – Recursive Function

Python Özyineli Fonksiyonlar – Recursive Function

Bir önceki makalede fonksiyon kavramını gördük. Python dilinde bir fonksiyon başka bir fonksiyonu çağırabilir. Fakat burada göreceğimiz bir fonksiyonun kendisini çağırabilmesidir. Kendisini çağırabilen fonksiyonlara özyineli (recursive) fonksiyon adı verilir.

Program: Faktöriyel hesabını özyineli fonksiyon yardımıyla yapalım.

def fakt(n):
    if n==1:
        return 1
    else:
        return n*fakt(n-1)

print("5!=", fakt(5))
print("8!=", fakt(8))
print("1!=", fakt(1))

Çıktı:

5!= 120
8!= 40320
1!= 1

026_fonksiyon.py

Program: Fibonacci sayısının üretilmesinde özyineli fonksiyon kullanalım.

def fibon(n):
    if n==1:
        return 1
    elif n==2:
        return 1
    else:
        return fibon(n-1)+fibon(n-2)

for i in range(1,20):
    print(fibon(i), end=" ")

print()

Çıktı:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

027_fonksiyon.py

Program: Pascal Üçgenini Özyineli Fonksiyon ile oluşturalım.

def pascal(n):
    if n == 1:
        return [1]
    else:
        SAT = [1]
        ON_SAT = pascal(n-1)
        for i in range(len(ON_SAT)-1):
            SAT.append(ON_SAT[i] + ON_SAT[i+1])
        SAT += [1]
    return SAT
for i in range(1,11):
    print(pascal(i))

Çıktı:

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

028_fonksiyon.py

Leave A Comment

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir