In our last course, we learned about loops to repeat actions. Maybe we could reuse entire chunks of code across different parts of our program.
Engagement Message
Have you ever wished you could copy-paste code but make it smarter?
That's where functions come in! A function is like a mini-program inside your main program. You write the code once, give it a name, and then "call" it whenever you need it.
Think of it like having a helpful assistant.
Engagement Message
What would you name yours?
Functions help us avoid writing the same code over and over. Instead of copying and pasting, we create a function once and use it multiple times. This makes our code cleaner and easier to fix.
Engagement Message
Can you think of any repetitive tasks in your daily life?
Here's the basic structure of creating a function in Python. We start with def
, then the function name, parentheses, and a colon. The function's code goes on the next lines, indented.
Engagement Message
Notice how similar this looks to if
statements?
Let's create our first function! This one will print a simple greeting:
Engagement Message
We've defined the function, but nothing happens yet. Why do you think that is?
