6 of 6
Before running your function, check:
def and end the line with a colon :?return if you need to use the result later?# Wrong β this just defines it, never runs it def say_hi(): print("Hi!") # Right β call it! def say_hi(): print("Hi!") say_hi()
# Wrong β prints but doesn't give back the value def add(a, b): print(a + b) result = add(3, 5) # result is None! # Right β return gives the value back def add(a, b): return a + b result = add(3, 5) # result is 8
# Wrong β anything after return is skipped def add(a, b): return a + b print("Done!") # This NEVER runs # Right β put print before return def add(a, b): print("Done!") return a + b
def greet(name, age): print(f"Hi {name}, you are {age}") greet("Alex") # Error! Missing age greet("Alex", 15) # Correct
# If you PRINT inside the function: def double(n): print(n * 2) double(5) # Shows: 10 result = double(5) # Shows: 10, but result is None print(f"Answer: {result}") # Shows: Answer: None (oops!) # If you RETURN inside the function: def double(n): return n * 2 result = double(5) # Nothing printed, result is 10 print(f"Answer: {result}") # Shows: Answer: 10
| Situation | Use |
|---|---|
| You need to use the result in more code | return |
| You just want to display something | print |
| You want to save the result in a variable | return |
| You want to do math with the result | return |
| You're making a message to show the user | print |
TypeError: greet() takes 1 positional argument but 2 were given
You called the function with too many arguments.
TypeError: greet() missing 1 required positional argument: 'name'
You called the function without enough arguments.
NameError: name 'my_function' is not defined
You either misspelled the function name or forgot to define it above where you're calling it.
β Standard library: heapq, collections, itertools, math, random, functools, datetime, bisect
β Functions, classes, recursion, print()
β No file system, subprocess, OS access, or network requests
β No pip install (all supported modules are pre-loaded)
β±οΈ 5 second execution time limit