Defining functions with def
15 minutes read
Beginner friendly
Includes practice
Why bother with functions?
Without functions you’ll quickly find yourself repeating blocks of code again and again.
Functions help you:
- Reuse common logic instead of duplicating code
- Organize problems into manageable chunks
- Name logic clearly so your code explains itself
Everyday analogy
- Think about how you tie your shoes or make a coffee
- You don’t think through every step each time—you follow a known sequence
- Functions let you name those steps and reuse them in your code
Defining your first function
Functions begin with the def keyword. You name the function, add parentheses (), and end with a colon. Then indent the body.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
When run, this prints Hello!
Important parts:
defstarts the function definitionsay_hellois the function name()— no input parameters for now:followed by indented lines (the function’s body)
Try editing the message inside print() to personalize it.
Adding parameters (inputs)
To make functions flexible, we use parameters—placeholders for input values.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Each time you call greet("Ada"), the string "Ada" is passed to the parameter name.
The function then prints "Hi Ada 😊" and so on.
Returning a value with return
The print() function only shows something on-screen.
Want your function to produce output you can reuse? Use return.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Once Python hits return, it exits the function immediately and hands back the result.
Mini Practice – Square That Number!
Practice Challenge
Write a function called squarethat takes one number and returns that number multiplied by itself. Then call square(4)and store the result in result.Finally, print the result to verify.
Define a function that returns n * n, then call square(4) and store the result.
Optional Parameters (Default Values)
Functions can have optional defaults. You can assign a value to certain parameters so they can be skipped when you call the function.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
⚠️ Defaults must come after any required parameters.
Practice – Average of Three Numbers
Practice Challenge
Write a function called avg_of_three(n1,n2,n3)that takes three numbers and returns their average. Then call the function with values 5,10,and 15,store the result in avg,and print it. The result should be 10.0.
Define avg_of_three(n1,n2,n3) that returns (n1 + n2 + n3) / 3 and store the result.
Quick Practice
2 minutesCan you spot common mistakes when using functions in Python?
What happens if you write say_hello instead of say_hello()?
What happens to code placed after a return in a function?
Quick Summary
Define functions using
def name():followed by indented codeFunctions can accept parameters and return results using
returnUse parentheses to call a function;
say_hi()vssay_hi
- You learned how to define and call simple functions using
def - You practiced using parameters and return statements for flexible logic
- You explored default values and wrote your first multi-argument function
- You identified common pitfalls like forgetting parentheses or misusing
return
AI Tutor
Solve first — tutor's here to guide.
Runtric might be wrong. Think critically.