Python Mastery – From Basics to Brilliance / Functions

What is a Function?

A function is a block of reusable code that performs a specific task. Instead of repeating code, define it once and call it whenever needed.


🔍 Why Use Functions?

  • ✅ Makes code reusable

  • ✅ Improves readability

  • ✅ Easier to test and debug


🛠️ Function Syntax

def function_name(parameters):
    # code block
    return result

💡 Example: Greet User

def greet(name):
    return f"Hello, {name}!"

print(greet("Alice")) # Hello, Alice!

🔄 Real-life Analogy

A function is like a coffee machine:

  • Press a button (call the function)

  • Coffee comes out (returns result)

  • You don’t need to know the internal workings every time ☕️