In our last course, we learned about loops to repeat actions. But what if we want to 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.
Engagement Message
Think of it like having a helpful assistant - 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 Go. We start with func
, then the function name, parentheses, and curly braces. The function's code goes between the braces.
Engagement Message
Notice how the structure is similar to how we write if
and for
statements?
Let's create our first function! This one will print a simple greeting:
Engagement Message
We've defined the function, but with just this code nothing will be printed. Why do you think that is?
