# Functions

Functions let you **group code together** so it can be reused. Think of them as mini-programs inside your program.

A function is a **block of code** that only runs when you **call** it. It can take inputs (called parameters) and return an output (using `return`).

## Defining a Function

To define a function, we use the `def` keyword.

Syntax:

```python
def function_name():
    # code block
```

Example:

```python
def greet():
    print("Hello NCA!")

greet()  # calling the function
# Output: Hello NCA!
```

## Parameters and Arguments

Parameters are the values you define inside the function. Arguments are the actual values you pass when calling it.

Example:

```python
def greet(name): # Parameter we define inside the function.
    print("Hello,", name)

greet("NCA") # String Argument we passed.

# Output: Hello, NCA
```

You can pass **multiple parameters**:

```powershell
def add(a, b):
    print(a + b)

add(3, 5)
# Output: 8
```

{% hint style="info" %}
Again, we can literally pass any data types and not just strings or integers.
{% endhint %}

## Return Statement

Functions can return a value using `return`.

```python
def add(a, b):
    return a + b

result = add(4, 6)
print(result)
# Output: 10
```

## Default Parameters

You can give a **default value** to parameters.

```python
def greet(name="Guest"):
    print("Hello,", name)

greet()         # Output: Hello, Guest
greet("Zara")   # Output: Hello, Zara
```

## Keyword Arguments

You can specify arguments by name.

```python
def introduce(name, age):
    print(f"{name} is {age} years old.") # We used f-string here.

introduce(age=25, name="John")
# Output: John is 25 years old.
```

## Variable-Length Arguments

Sometimes, you don’t know how many arguments will be passed. Python provides two special symbols:

* `*args` for non-keyword variable arguments (like a list)
* `**kwargs` for keyword variable arguments (like a dictionary)

### `*args`&#x20;

```python
def add_all(*numbers):
    print(sum(numbers))

add_all(1, 2, 3, 4)
# Output: 10
```

### `**kwargs`

```python
def show_info(**info):
    for key, value in info.items():
        print(key, ":", value)

show_info(name="Alex", age=30)
# Output:
# name : Alex
# age : 30
```

## Function Scope

Variables inside a function are **local** to that function.

```python
def my_func():
    x = 10  # local variable
    print(x)

my_func()
# Output: 10

# print(x)  # Error! x is not defined outside the function
```

## Global Keyword

If you want to use and modify a **global variable** inside a function:

```python
x = 5

def change():
    global x
    x = 10

change()
print(x)
# Output: 10
```

## Lambda Functions (Anonymous Functions)

Short functions you define in one line using `lambda`.

Syntax:

```python
lambda arguments: expression
```

Example:

```python
square = lambda x: x * x
print(square(5))
# Output: 25
```

## Docstrings

Use triple quotes (`"""`) inside a function to write a **docstring** (documentation string).

```python
def greet():
    """This function greets the user."""
    print("Hello!")

print(greet.__doc__) # To print the docstring
# Output: This function greets the user.
```

## Nested Functions

Functions defined inside other functions.

```python
def outer():
    def inner():
        print("Inside inner function")
    inner()

outer()
# Output: Inside inner function
```


---

# 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://handbook.ncateam.xyz/fundamentals/python/functions.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.
