Functions in Go
# CHAPTER 9
Functions in Go
1. Introduction
If you write all your code insidemain(), your program will quickly become an unreadable, 5,000-line mess. Functions allow you to break your code into small, reusable, and testable blocks. A function takes inputs, performs a specific task, and returns outputs.
2. Learning Objectives
By the end of this chapter, you will be able to:- Declare and call basic functions.
- Pass parameters into functions.
- Return values from functions.
- Utilize Go's unique feature: Multiple Return Values.
- Understand Variadic functions.
3. Declaring and Calling a Function
You define a function using thefunc keyword, followed by the function name, parentheses (), and curly braces {}.
4. Parameters and Arguments
You can pass data *into* a function. The variables in the definition are called Parameters. The actual data you pass in is called the Argument.Syntax Shortcut: If multiple parameters share the same type, you only need to write the type once at the end!
5. Return Values
A function can perform a calculation and hand the result *back* to the code that called it using thereturn keyword. You must specify the return data type in the function signature.
6. Multiple Return Values (Go's Superpower)
In C++ or Java, a function can only return ONE thing. If you want to return two things, you have to create a complex object or array.Go allows functions to return multiple values natively! This is heavily used in Go for Error Handling (returning the result AND an error status).
7. The Blank Identifier _
What if a function returns two values, but you only care about the first one? Go throws a compiler error if you declare a variable and don't use it.
To ignore a return value, use the Blank Identifier (_).
8. Named Return Values
You can name your return variables directly in the function signature. Go will automatically declare them, and a barereturn will send them back.
*Note: Use naked returns sparingly, as they can hurt readability in long functions.*
9. Variadic Functions
Sometimes you don't know how many arguments will be passed. A variadic function accepts any number of arguments using....
*(Fun Fact: fmt.Println is a variadic function, which is why you can pass it infinite arguments separated by commas!)*
10. Common Mistakes
-
Forgetting to return: If your function signature promises an
int(func getAge() int), you MUST have areturnstatement that yields an integer.
-
Scope Issues: A variable declared inside
func A()cannot be seen or used byfunc B(). This is called Local Scope.
11. Best Practices
- Single Responsibility Principle: A function should do exactly ONE thing. If it validates a user, saves to the database, and sends an email, break it down into three separate functions.
- Keep functions small: If a function is over 50 lines long, consider refactoring it.
12. Exercises
-
1.
Write a function called
dividethat takes twofloat64numbers.
-
2.
Have it return the division result. If the second number is 0, return
0(we will handle proper errors later). Test it inmain.
13. MCQs with Answers & Explanations
What keyword is used to define a function in Go?
Variables defined inside the function signature parentheses are called:
Actual values passed to a function during a call are called:
How do you specify that a function returns two integers?
What does the blank identifier (_) do?
What does numbers ...int mean in a function signature?
What happens if a function promises to return an int but lacks a return statement?
If you have parameters (x int, y int), what is the syntax shortcut?
What is a "naked return"?
14. Interview Preparation
Interview Questions:- 1. Why does Go support multiple return values, and what is its most common use case? (Answer: Returning the actual result AND an error status simultaneously).
-
2.
Explain what the blank identifier
_is used for in function returns.