📖 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.
Practice
(1/5)
1. Why do we use functions in Go programming? func keyword defines a function.
easy
A. To make the program run faster
B. To write code only once without running it
C. To avoid using variables
D. To organize code into reusable blocks
Solution
Step 1: Understand the purpose of functions
Functions group code that performs a task so it can be reused easily.
Step 2: Compare options with function benefits
Only organizing code into reusable blocks matches the main reason for functions.
Final Answer:
To organize code into reusable blocks -> Option D
Quick Check:
Functions = reusable code blocks [OK]
Hint: Functions help reuse code easily [OK]
Common Mistakes:
Thinking functions make code run faster
Confusing functions with variables
Believing functions prevent code execution
2. Which of the following is the correct way to define a function in Go?
easy
A. func greet() { fmt.Println("Hello") }
B. function greet() { fmt.Println("Hello") }
C. def greet() { fmt.Println("Hello") }
D. func greet[] { fmt.Println("Hello") }
Solution
Step 1: Recall Go function syntax
Go uses func keyword followed by name and parentheses.
Step 2: Check each option's syntax
Only func greet() { fmt.Println("Hello") } uses correct Go syntax: func greet() { ... }.
C. Missing parentheses after function name in definition
D. Function call syntax is wrong
Solution
Step 1: Check function definition syntax
Function definitions must have parentheses even if no parameters are used.
Step 2: Verify other parts
The function call sayHello() is correct. The package declaration and import are present, so the only syntax error is the missing parentheses after the function name in the definition.
Final Answer:
Missing parentheses after function name in definition -> Option C
Quick Check:
Function definition needs () [OK]
Hint: Function definitions always need () after name [OK]
Common Mistakes:
Ignoring missing parentheses in function definition
Thinking function call syntax is wrong
Believing the code has no errors
5. You want to print a greeting message multiple times without repeating code. Which approach best uses functions in Go?
hard
A. Write the print statement many times in main
B. Create a function that prints the message and call it repeatedly
C. Use a variable to store the message but print once
D. Write the print statement inside a loop without a function
Solution
Step 1: Understand the goal
We want to avoid repeating the same print code multiple times.
Step 2: Evaluate options for reusability
Only creating a function and calling it repeatedly avoids code repetition and organizes code well.
Final Answer:
Create a function that prints the message and call it repeatedly -> Option B
Quick Check:
Functions avoid repeating code [OK]
Hint: Use functions to avoid repeating code [OK]
Common Mistakes:
Repeating print statements instead of using functions
Using variables without reusing code
Ignoring functions and relying only on loops
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.