0
0
Goprogramming~15 mins

Why functions are needed in Go - See It in Action

Choose your learning style9 modes available
Why functions are needed
📖 Scenario: Imagine you are baking cookies and you need to mix ingredients multiple times. Instead of mixing each time from scratch, you create a simple step called "mixing" that you can use whenever needed. This saves time and keeps your work neat.
🎯 Goal: You will create a small Go program that uses a function to repeat a task. This shows why functions help us avoid repeating code and make programs easier to read and change.
📋 What You'll Learn
Create a function called greet that prints a greeting message
Call the greet function three times
Use fmt.Println to print messages
Show how functions help avoid repeating the same code
💡 Why This Matters
🌍 Real World
Functions are like small helpers in real life. For example, a recipe step you repeat many times is like a function. It saves time and keeps things organized.
💼 Career
Knowing how to use functions is essential for any programming job. It helps you write clean, reusable code that others can understand and maintain easily.
Progress0 / 4 steps
1
Create a simple greeting message
Write a line of code that prints "Hello, welcome!" using fmt.Println.
Go
Hint

Use fmt.Println("Hello, welcome!") inside the main function.

2
Create a function called greet
Create a function named greet that prints "Hello, welcome!" using fmt.Println. Add this function above main.
Go
Hint

Define func greet() and put fmt.Println("Hello, welcome!") inside it.

3
Call the greet function three times
Remove the fmt.Println line from main. Instead, call the greet function three times inside main.
Go
Hint

Inside main, write greet() three times, one after another.

4
Print the output to see the repeated greetings
Run the program to print the greeting message three times by calling greet() three times.
Go
Hint

Run the program and see the greeting printed three times.