Python Functions
Teaching Code to Reuse Itself
Imagine you’re a hacker writing scripts to automate server tasks. You notice the same block of code repeating again and again. Repetition wastes time, clutters your program, and makes debugging painful. What if you could wrap that logic into a reusable tool? That’s what functions are: named blocks of code that perform a specific task, reusable whenever you need them.
Functions are the DNA of modular programming. They let you break complex problems into smaller, reusable pieces. This chapter is about learning how to define functions, call them, and pass parameters to make them flexible.
Why Functions Matter
- Abstraction: Functions hide complexity. You don’t need to know how they work internally to use them.
- Reusability: Write once, use many times. Functions eliminate redundancy.
- Maintainability: If logic changes, update the function once instead of everywhere.
- Parameters: Functions become flexible when they accept inputs, adapting to different scenarios.
- Return Values: Functions can send results back, making them powerful building blocks.
Defining Functions
def greet():
print("Hello, Hacker!")
- Why?
defintroduces a function definition. The name (greet) identifies it, and the block inside defines its behavior.
Calling Functions
greet() # Output: Hello, Hacker!
- Why? Calling executes the function. Without calling, the function just sits idle, waiting to be used.
Parameters – Flexible Functions
def greet(name):
print(f"Hello, {name}!")
greet("Shubham") # Output: Hello, Shubham!
- Why? Parameters let you customize behavior. Instead of hard‑coding, you pass values dynamically.
Parameters & Return Values
def add(a, b):
return a + b
result = add(10, 5)
print("Sum:", result)
- Why? Functions can accept multiple inputs and return results. This makes them versatile problem‑solvers.
Default Parameters
def greet(name="Hacker"):
print(f"Hello, {name}!")
greet() # Output: Hello, Hacker!
greet("Shubham") # Output: Hello, Shubham!
- Why? Default parameters provide fallback values, making functions more robust.
Keyword Arguments
def introduce(name, role):
print(f"My name is {name}, and I am a {role}.")
introduce(role="Engineer", name="Shubham")
- Why? Keyword arguments improve readability and flexibility when calling functions.
The Hacker’s Notebook
- Functions are reusable blocks of code - your tools for modular programming. Defining with
defcreates the tool; calling executes it. - Parameters make functions flexible, adapting to different inputs. Return values transform functions into problem‑solvers, not just executors.
Hacker’s Mindset: treat functions as your arsenal of reusable hacks. Each one is a weapon you can deploy whenever needed, saving time and reducing clutter.

Updated on Jan 2, 2026