Common Functions Mistakes
The problem...
Your function doesn't return what you expect. Or it crashes on valid input. Or it changes a variable that shouldn't change.
These are the mistakes almost every beginner makes with functions. At least once.
Mistake 1 — Calling a function before defining it
# Wrong — NameError
greet("Bull")
def greet(name):
print(f"Hello, {name}.")
# Right — define before calling
def greet(name):
print(f"Hello, {name}.")
greet("Bull")
Python reads top to bottom. The function must exist before you call it.
Mistake 2 — Forgetting parentheses when calling
def greet():
print("Hello.")
greet # does nothing — references the function object
greet() # calls it — prints "Hello."
Without parentheses — you reference the function, not call it. Nothing runs.
Mistake 3 — Forgetting return
# Wrong — returns None
def calculate_bmi(weight, height):
bmi = weight / height ** 2
print(bmi) # prints but doesn't return
result = calculate_bmi(70, 1.75)
print(result) # None
# Right
def calculate_bmi(weight, height):
return weight / height ** 2
result = calculate_bmi(70, 1.75)
print(result) # 22.857...
Printing inside a function is not the same as returning. return sends the value out. print just displays it.
Mistake 4 — Code after return
def check(number):
if number > 0:
return "positive"
print("This never runs.") # unreachable
return "zero or negative"
return stops the function immediately. Everything after it in the same block is unreachable.
Mistake 5 — Default parameters before required ones
# Wrong — SyntaxError
def greet(name="stranger", role):
print(f"{name} — {role}")
# Right
def greet(role, name="stranger"):
print(f"{name} — {role}")
Required parameters always come first. Default parameters always come last.
Mistake 6 — Modifying a global variable inside a function
count = 0
def increment():
count = count + 1 # UnboundLocalError
# Right — use return instead
def increment(count):
return count + 1
count = increment(count)
Inside a function, assigning to a variable creates a local copy. The global is untouched — and Python gets confused. Use parameters and return instead.
What's really happening
Most function mistakes come from assumptions — that Python reads out of order, that print equals return, that globals are freely modifiable. They're not.
Functions have clear rules. Know them.
The mindset shift
Stop thinking: "The function knows everything."
Start thinking: "The function knows only what you give it — and sends back only what you return."
What you should understand now
- Define before calling — Python reads top to bottom
- Parentheses are required to call a function
printinside a function is notreturnreturnstops the function — nothing after it runs- Default parameters always come last
- Use parameters and
return— avoid modifying globals