Documentation Generator | Python

Fibonacci Recursive Function

This Python function, "fibonacci", calculates the nth Fibonacci number using recursive approach for non-negative integers.


Empty image or helper icon

Prompt

def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n-1) + fibonacci(n-2)

Answer

# Function: fibonacci
# Purpose: Calculate the nth Fibonacci number using recursive approach.
# Parameters:
#   - n (int): The Fibonacci number to calculate.
# Returns:
#   - int: The nth Fibonacci number.
# Notes:
#   - Assumes n is a non-negative integer.

def fibonacci(n):
    """
    Calculate the nth Fibonacci number using recursive approach.

    Parameters:
    n (int): The Fibonacci number to calculate.

    Returns:
    int: The nth Fibonacci number.

    Notes:
    Assumes n is a non-negative integer.
    """
    if n <= 1:
        return n
    else:
        return fibonacci(n-1) + fibonacci(n-2)

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This Python function, "fibonacci", calculates the nth Fibonacci number using recursive approach for non-negative integers.