### Week 12: Advanced Functions In Python
>By Akinlade Temitope Victory
This week we went deeper into functions by exploring some advanced concepts. The focus was on parameterised functions, composite functions, positional parameters, default parameters, and arguments. Understanding these areas makes our code more reusable, adaptable, and efficient.
#### PARAMETERISED FUNCTIONS
A parameterised function is one that receives information when called. The parameters serve as placeholders for actual values that will later be supplied as inputs.
Example:
```python
def greet(name):
print(f"Hello, {name}!")
greet("Alice") # Output: Hello, Alice!
```
#### COMPOSITE FUNCTIONS
Composite functions are simply functions that make use of other functions within them. They allow us to build more complex logic by combining smaller, reusable pieces of code.
Example:
```python
def square(x):
return x * x
def double_square(y):
return square(y) * 2
print(double_square(3)) # Output: 18
```
#### POSITIONAL PARAMETERS
In Python, function arguments are matched to parameters by their order. This means the sequence matters because each argument is assigned to a parameter based on position.
Example:
```python
def divide(a, b):
return a / b
print(divide(10, 2)) # Output: 5.0
print(divide(2, 10)) # Output: 0.2
```
#### DEFAULT PARAMETERS
Default parameters are values given to parameters beforehand. If the function is called without supplying a value, the default one is used.
Example:
```python
def greet(name="Guest"):
print(f"Welcome, {name}!")
greet() # Output: Welcome, Guest!
greet("Alice") # Output: Welcome, Alice!
```
#### ARGUMENTS (*args and **kwargs)
Arguments are the real values passed when a function is executed. Python provides two special ways to handle them:
*args : allows multiple positional arguments.
**kwargs : allows multiple keyword arguments.
Example:
```python
def add_numbers(*args):
return sum(args)
print(add_numbers(1, 2, 3, 4)) # Output: 10
def show_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
show_info(name="Alice", age=25)
# Output:
# name: Alice
# age: 25
```
CONCLUSION
This week’s lessons on advanced function concepts like parameterised functions, composite functions, positional and default parameters, and arguments gave us more tools to write flexible and efficient code. With these, functions become more dynamic and can easily adapt to different inputs. Thanks for following along, until next time!