Programming FundamentalsNot Started
Functions
Define reusable blocks of code with parameters and return values
0%
Knowledge0%
Learn & DrillFluency0%
Drill & SpeedRetention0%
Mastery & ReviewConfidence0%
All modesKnowledge
Fluency
Retention
Knowledge Debt detected
You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.
Explanation
A function is a reusable block of code that performs a specific task. You define it once and call it many times.
python
def add(a, b):
return a + b
result = add(3, 5)
print(result) # 8The `def` keyword
- Starts every function definition, followed by a name, parentheses, and colon:
def function_name(): - The indented block below is the function body — everything at that indentation level belongs to the function
python
def greet():
print("Hello, world!")Parameters
python
def add(a, b):
return a + baandbare — the inputs the function expects
Arguments
3and5are — the actual values passed when you call the function
python
add(3, 5)- Parameters are the names in the definition; arguments are the values passed at call time
`return`
returnsends a value back to the caller, which can be stored or used directly
def add(a, b):
return a + b
result = add(2, 3)
print(result)
print(add(10, 5))What will this print?
No `return` → `None`
- Functions without a
returnstatement return None by default - The code runs, but no value is sent back
def log_message(message):
print(message)
result = log_message("hi")
print(result)What will this print?
Functions help you avoid repeating code (DRY — Don't Repeat Yourself).
Examples
Function with default parameter
Default parameter values make arguments optional
def total(price, shipping=5):
return price + shipping
print(total(20)) # 25 (shipping defaults to 5)
print(total(20, 10)) # 30How well did you understand this?
Next in Programming Fundamentals
Loops