# Base case and recursive case

Rekursiv funktsiya o'zini chaqirganligi sababli, cheksiz tsikl bilan tugaydigan funktsiyani noto'g'ri yozish oson. Misol uchun, siz ortga hisoblashni chop etuvchi funktsiyani yozmoqchisiz, deylik:

![Key](/files/ac6N4jMfGFKePHSdgA4Y)

> `> 3...2...1`

Siz uni rekursiv tarzda yozishingiz mumkin, masalan:

### Python

```python
def countdown(i):
    print i
    countdown(i-1)
```

### Golang

```go
func countdown(i int) {
    fmt.Println(i)
    countdown(i-1)
}
```

Ushbu kodni yozing va uni ishga tushiring. Muammoni sezasiz: bu funksiya abadiy ishlaydi!

![Infinitive loop](/files/kKlx93j3ONvCh9mIc13a)

> `> 3...2...1...0...-1...-2...-3...`

(Skriptingizni o'chirish uchun Ctrl-C tugmalarini bosing.) Rekursiv funktsiyani yozganingizda, uni qachon takrorlashni to'xtatish kerakligini aytishingiz kerak. Shuning uchun har bir rekursiv funktsiya ikki qismdan iborat: asosiy va rekursiv holat. Rekursiv holat - bu funksiya o'zini chaqirganda. Asosiy holat - bu funktsiya o'zini qayta chaqirmasa ... shuning uchun u cheksiz tsiklga kirmaydi.

Ortga hisoblash funksiyasiga asosiy registrni qo'shamiz:

![Base case](/files/OphS1JDrskckkHXPXwya)

### Python

```python
def countdown(i):
    print i
    if i <= 0:
        return
    else:
        countdown(i-1)
```

### Golang

```go
func countdown(i int) {
    fmt.Println(i)
    if i <= 0 {
        return
    } else {
        countdown(i-1)
    }
}
```

Endi funksiya kutilganidek ishlaydi. Bu shunga o'xshash narsaga o'tadi.

![Recursive](/files/Je9iAboXJyrhpaGb4ltq)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://grokking.realtemirov.uz/3.-recursion/2.-base-case-and-recursive-case.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
