4 min read
Functions
Prerequisites: Variables
A function is a reusable, named block of code that performs a specific task — written once, called as many times as needed.
Syntax
def function_name(parameter):
...
Example
def greet(name):
print(f"Hello, {name}!")
greet("Ada")
greet("Grace")
Expected output
Hello, Ada!
Hello, Grace!
Common mistakes
- Forgetting the parentheses when calling a function —
greetrefers to the function itself,greet("Ada")actually runs it.