> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/piscine-life/c05/ft_recursive_power.md).

# ft\_recursive\_power

**Objective:**

Create a **recursive function** that calculates the power of a given number `nb` raised to the exponent `power`. The function should return **0** if the exponent is negative and **1** if the exponent is **0**.

**Turn-in Directory:**

ex03/

**Files to Turn In:**

ft\_recursive\_power.c

**Allowed Functions:**

None

**Prototype:**

```c
int ft_recursive_power(int nb, int power);
```

***

#### **Detailed Explanation**

The **ft\_recursive\_power** function calculates the value of `nb` raised to the power of `power` using recursion. For example:

* `2^3 = 2 * 2 * 2 = 8`
* `5^0 = 1` (any number raised to the power of 0 is 1)
* `3^-2 = 0` (negative exponents are not handled, so the function returns 0)

The function returns:

* The result of `nb^power` if `power` is a non-negative integer.
* **1** if `power` is **0**.
* **0** if `power` is negative.

***

#### **Code Implementation**

```c
int ft_recursive_power(int nb, int power)
{
    int result;

    result = nb;
    if (power < 0)  // Negative exponents are not handled
        return (0);
    if (power == 0)  // Any number raised to the power of 0 is 1
        return (1);
    else if (power > 1)  // Recursive calculation of power
        result = result * ft_recursive_power(nb, power - 1);
    return (result);
}
```

***

#### **Example Usage**

```c
#include <stdio.h>

int ft_recursive_power(int nb, int power);

int main(void)
{
    printf("2^3: %d\n", ft_recursive_power(2, 3));  // Output: 8
    printf("5^0: %d\n", ft_recursive_power(5, 0));  // Output: 1
    printf("3^-2: %d\n", ft_recursive_power(3, -2)); // Output: 0
    printf("10^5: %d\n", ft_recursive_power(10, 5)); // Output: 100000

    return 0;
}
```

***

#### **Edge Cases to Consider**

1. **Power of 0**:
   * Input: `5^0` → Returns **1** (any number raised to the power of 0 is 1).
2. **Negative Power**:
   * Input: `3^-2` → Returns **0** (negative exponents are not handled).
3. **Positive Power**:
   * Input: `2^3` → Returns **8** (`2 * 2 * 2 = 8`).
4. **Large Power**:
   * Input: `10^5` → Returns **100000** (`10 * 10 * 10 * 10 * 10 = 100000`).

***

#### **Limitations of ft\_recursive\_power**

1. **No Handling of Negative Exponents**:
   * The function does not handle negative exponents and returns **0** for such cases.
2. **No Handling of Overflows**:
   * The function does not handle integer overflows. For large values of `nb` and `power`, the result may exceed the range of an `int`, leading to undefined behavior.
3. **Recursion Depth**:
   * For very large values of `power`, the function may cause a stack overflow due to excessive recursion depth.

***

#### **How It Works**

1. **Base Case 1 (Negative Power)**:

   * If `power` is negative, the function immediately returns **0** (negative exponents are not handled).

   ```c
   if (power < 0)
       return (0);
   ```
2. **Base Case 2 (Power of 0)**:

   * If `power` is **0**, the function returns **1** (since any number raised to the power of 0 is 1).

   ```c
   if (power == 0)
       return (1);
   ```
3. **Recursive Case**:

   * For any positive integer greater than 1, the function calls itself with `power - 1` and multiplies the result by `nb`.

   ```c
   else if (power > 1)
       result = result * ft_recursive_power(nb, power - 1);
   ```
4. **Return the Result**:

   * Once the recursion ends, the function returns the value of `result`, which is `nb^power`.

   ```c
   return (result);
   ```

***

#### **Key Points to Remember**

1. **Recursive Approach**:
   * The function uses recursion to calculate the power, calling itself with a smaller value of `power` until it reaches the base case.
2. **Base Cases**:
   * The base cases handle edge cases like `power = 0` and `power < 0`.
3. **Efficiency**:
   * The time complexity is **O(power)**, where `power` is the exponent. Each recursive call reduces `power` by 1 until it reaches **0**.

***

#### **Best Practices**

1. **Input Validation**:
   * Always check for invalid inputs (e.g., negative exponents) to avoid unexpected behavior.
2. **Edge Cases**:
   * Test edge cases like `power = 0`, `power < 0`, and large values of `power` to ensure the function behaves as expected.
3. **Overflow Handling**:
   * Consider adding overflow handling for large inputs, though it is not required in this exercise.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://42-guide.gitbook.io/42-guide/piscine-life/c05/ft_recursive_power.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.
