Building Reusable Superpowers
Part of the free Generative AI course on LogicWiz, module: Leveling Up Your Powers.
Episode 5: Building Reusable Superpowers
"Write it once. Use it forever. That's how real AI systems are built."
Nova Needs Abilities
You've taught Nova to store data, manipulate text, make decisions, and loop through collections. But you've been writing everything inline — one-off code that can't be reused.
In the real world, Nova will need the same abilities over and over:
- Clean text before every prompt
- Build prompts in a consistent format
- Call AI APIs with the same pattern
- Transform data across hundreds of inputs
Functions are how you give Nova permanent abilities — write the logic once, and she can use it forever.
Your First Superpower: Defining a Function
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
Key parts:
defstarts a function definition- parameters are in the parentheses (like
name) returnsends a value back to the caller- Call the function by name with parentheses:
greet("Alice")
{{visual:func-def-walkthrough}}
{{cell:l5-try-function}}
Parameters vs Arguments
When Nova calls a function, she needs to pass the right inputs. Let's clarify the terminology:
- Parameters: variables in the function definition (the placeholders)
- Arguments: actual values you pass when calling the function
def subtract(a, b): # a, b are parameters
return a - b
print(subtract(10, 5)) # 10, 5 are arguments
Positional vs Keyword Arguments
print(subtract(10, 5)) # positional: order matters
print(subtract(b=10, a=5)) # keyword: order doesn't matter
Keyword arguments are safer and more readable, especially when calling functions from other modules.
{{visual:args-kwargs-walkthrough}}
{{cell:l5-try-args-vs-kwargs}}
Default Parameters
def greet(name, message="Hello"):
return f"{message}, {name}!"
print(greet("Alice")) # Hello, Alice!
print(greet("Bob", "Hi")) # Hi, Bob!
Best practice:
- Put required parameters first
- Put default parameters after
Flexible Inputs: Variable-Length Arguments
Sometimes Nova won't know how many inputs she'll receive — maybe 3 search results, maybe 30. Variable-length arguments handle this elegantly.
*args (Positional)
Collects extra positional arguments into a tuple:
def multiply(*args):
print(type(args)) # <class 'tuple'>
result = 1
for num in args:
result *= num
return result
print(multiply(1, 2, 3, 4)) # 24
You can pass any number of positional arguments. Inside the function, args is a tuple you can loop over.
**kwargs (Keyword)
Collects extra keyword arguments into a dictionary:
def print_info(**kwargs):
print(type(kwargs)) # <class 'dict'>
for k, v in kwargs.items():
print(f"{k}: {v}")
print_info(name="Alice", city="New York")
Inside the function, kwargs is a dictionary. You can use .get(), .items(), etc.
Combining *args and **kwargs
You can use both in the same function. Positional args come first:
def flexible(a, b, *args, **kwargs):
print(f"a={a}, b={b}")
print(f"extra positional: {args}")
print(f"extra keyword: {kwargs}")
flexible(1, 2, 3, 4, x=10, y=20)
{{visual:varargs-walkthrough}}
{{cell:l5-try-varargs}}
Return vs Print: A Critical Distinction
This trips up beginners constantly, and it matters for Nova. When Nova processes data, she needs values she can pass to the next step — not just text on a screen.
returngives a value back to the caller — the value can be stored, used in expressions, or passed to other functionsprintdisplays output to the screen (useful for debugging but doesn't produce a usable value)
def add_return(a, b):
return a + b
def add_print(a, b):
print(a + b)
result = add_return(3, 4) # result is 7
result2 = add_print(3, 4) # prints 7, but result2 is None
In production code, prefer returning values and let the caller decide what to print.
Mission 5: Give Nova Her First Abilities
Time to build Nova's function toolkit:
- Transform text cleanly
- Use default parameters
- Use
*args/**kwargsfor flexible inputs - Understand the difference between return and print
Complete this mission to earn the title: Knowledge Architect
Next up: You've been building everything from scratch. But Python has a massive ecosystem of pre-built tools — thousands of libraries that other developers have already perfected. In the next episode, you'll unlock this arsenal and meet Pandas, the library that makes data feel effortless...