What if you could write a piece of code once and use it everywhere without copying it?
Why Function declaration in Go? - Purpose & Use Cases
Imagine you need to calculate the area of many rectangles in your program. Without functions, you would have to write the same calculation code over and over again everywhere you need it.
Writing the same code repeatedly is slow and tiring. It's easy to make mistakes or forget to update all copies if the calculation changes. This makes your program messy and hard to fix.
Function declaration lets you write the calculation once, give it a name, and then use that name whenever you need the calculation. This keeps your code clean, easy to read, and simple to update.
area := width * height // repeated everywhere
func area(width, height int) int {
return width * height
}
result := area(5, 10)Functions let you organize your code into reusable blocks, making programs easier to build, understand, and maintain.
Think of a coffee machine: you press a button (call a function) to get coffee instead of making it from scratch every time.
Functions save you from repeating code.
They make your program cleaner and less error-prone.
Functions help you organize and reuse your work easily.
Practice
Solution
Step 1: Recall Go function syntax
In Go, functions are declared using the keywordfunc.Step 2: Compare options
Only func usesfunc, which is correct for Go.Final Answer:
func -> Option AQuick Check:
Function declaration keyword = func [OK]
- Using 'function' like JavaScript
- Using 'def' like Python
- Using 'fun' which is invalid
add that takes two integers and returns an integer?Solution
Step 1: Understand Go function parameter syntax
In Go, parameters are declared asname type. Soa int, b intis correct.Step 2: Check return type and function keyword
Function must start withfuncand specify return typeint. func add(a int, b int) int {} matches this exactly.Final Answer:
func add(a int, b int) int {} -> Option DQuick Check:
Parameter syntax = name type [OK]
- Putting type before name in parameters
- Using 'function' keyword instead of 'func'
- Omitting return type when needed
package main
import "fmt"
func multiply(x int, y int) int {
return x * y
}
func main() {
fmt.Println(multiply(3, 4))
}Solution
Step 1: Understand function call and return
The functionmultiplytakes two integers and returns their product.Step 2: Calculate multiply(3, 4)
3 * 4 = 12, so the function returns 12, which is printed.Final Answer:
12 -> Option AQuick Check:
3 times 4 = 12 [OK]
- Adding instead of multiplying
- Concatenating numbers as strings
- Expecting syntax error due to function
func greet(name string) string {
return "Hello, " + name
Solution
Step 1: Check function syntax
The function starts withfunc, has parameter and return type correctly.Step 2: Look for missing parts
The function body is missing the closing brace}to end the function.Final Answer:
Missing closing brace '}' -> Option CQuick Check:
Every function must end with '}' [OK]
- Forgetting to close function body with '}'
- Confusing parameter types
- Omitting return type
filterEven that takes a slice of integers and returns a new slice containing only even numbers?Solution
Step 1: Check parameter and return types
The function must take a slice of integers[]intand return a slice of integers[]int. func filterEven(nums []int) []int { var evens []int for _, n := range nums { if n%2 == 0 { evens = append(evens, n) } } return evens } matches this.Step 2: Verify logic to filter even numbers
func filterEven(nums []int) []int { var evens []int for _, n := range nums { if n%2 == 0 { evens = append(evens, n) } } return evens } creates a new slice, loops over input, appends even numbers, and returns the new slice. This is correct.Final Answer:
func filterEven(nums []int) []int { var evens []int for _, n := range nums { if n%2 == 0 { evens = append(evens, n) } } return evens } -> Option BQuick Check:
Filter slice by appending evens [OK]
- Wrong parameter or return types
- Returning inside loop prematurely
- Printing instead of returning filtered slice
Ready to prove it?
Pass the test and this pattern moves unknown
