What is a Function?
A function is a reusable block of code designed to perform a specific task. Instead of rewriting the same instructions multiple times throughout your program, you bundle them into a named function and execute them whenever needed!
Real-world analogy
Reusability
Write code once and run it as many times as you like from anywhere in your project.
Modular Design
Breaks large complex programs into small, easy-to-read, manageable pieces.
Clean Code
Eliminates redundant lines and keeps your codebase organized and readable.
Easy Debugging
Fix a bug inside a function once, and it automatically fixes it everywhere!
Key vocabulary
- Function Definition โ declaring the function's name, parameters, and code body using
def. - Function Call โ executing the function using its name followed by parentheses
func(). - Parameter โ a variable name specified in the function header to receive input data.
- Return Value โ the final output sent back by the function using
return.
๐งช Compact Function Playground
Run the script below to see how defining a function and calling it twice executes the code on demand:
๐งช Compact Function Playground
Ready to run
No variables yet.
Run your Python code to see them appear here live!
No variables yet.
Run your Python code to see them appear here live!