Make your own commands!
Learn how to create your own functions โ reusable blocks of code that you can call whenever you need them.
A function is a named block of code that does a specific job. You define it once, then call it by name whenever you want it to run. Functions help you avoid repeating code and keep your programs organized.
๐ก Think of it like this:
Think of a function like a recipe card. You write the recipe once (define the function), and then anytime you want that dish, you just follow the card (call the function). You do not rewrite the recipe every time!
Functions are the building blocks of real programs. Every app you use is made of hundreds or thousands of functions. Learning to write them is a huge step forward!
Where you see this in real life:
The print() function you have been using is just a built-in function! Someone at Python HQ wrote it so you do not have to.
A function is a named block of code that does a specific job. You define it once, then call it by name whenever you want it to run. Functions help you avoid repeating code and keep your programs organized.
Use the defkeyword to create a function. Then call it by writing its name with parentheses!
def creates the function. Calling it by name runs the code inside.๐ก We called the function 3 times โ but only wrote the code once!
Functions can do anything โ print, calculate, make decisions, loop, and more!
Click โถ Run to see the output!โ Without functions
You write the same code over and over. It gets messy and hard to fix.
โ With functions
You write it once and call it whenever you need it. Clean and easy to fix!
Which keyword do you use to create a function in Python?