Junior — Middle
Explain the meaning and purpose of functions in Python.
sobes.tech AI
Answer from AI
A function in Python is a named block of code that performs a specific task and can be called from other parts of the program. Functions allow:
- Reusing code without duplication.
- Structuring the program, making it more readable and maintainable.
- Accepting input parameters and returning results.
Example of a simple function:
def greet(name):
"""Prints a greeting for the given name."""
print(f"Hello, {name}!")
# Calling the function
greet('Anna') # Will output: Hello, Anna!