Functions in Go
Functions in Go are reusable blocks of code designed to perform specific tasks. They promote modularity, reusability, and cleaner program structure. A function can take zero or more parameters and can return one or more values.
Types of Function Calls
Go supports two ways to pass arguments to functions:
-
Call by Value: A copy of the actual parameter is passed to the function. Changes made inside the function do not affect the original variable.
-
Call by Reference: The reference (memory address) of the variable is passed. Any modification inside the function reflects in the original variable.
Syntax of a Function
func function_name(Parameter-list) (Return-type) {
// function body - executable statements
}
Explanation:
func→ Keyword to declare a function.function_name→ Name of the function (should be descriptive).Parameter-list→ Input values (if any).(Return-type)→ Specifies what type of value(s) the function returns.- The return type can be omitted if the function doesn’t return anything.
Example 1: Function to Add Two Numbers
package main
import "fmt"
func add(x int, y int) int {
return x + y
}
func main() {
fmt.Println(add(100, 200))
}
// OUTPUT: 300
Explanation:
- The function
addaccepts two integers (x,y) and returns their sum. - Both parameters are of type
int. - The
fmt.Printlnstatement prints the returned value.
Example 2: Function Returning Multiple Values
package main
import "fmt"
func swap(x, y string) (string, string) {
return y, x
}
func main() {
a, b := swap("Hello", "World")
fmt.Println(a, b)
}
// OUTPUT: World Hello
Explanation:
- Go supports returning multiple values from a function.
- The
swapfunction returns two strings in reversed order.
Example 3: Call by Reference (Using Pointers)
package main
import "fmt"
func updateValue(num *int) {
*num = *num + 50
}
func main() {
val := 100
updateValue(&val)
fmt.Println("Updated Value:", val)
}
// OUTPUT: Updated Value: 150
Explanation:
- The function
updateValuetakes a pointer to an integer. - Inside the function, we dereference it (
*num) to modify the actual value. - Since we passed the memory address (
&val), the original variable gets updated.
Example 4: Anonymous Function
package main
import "fmt"
func main() {
add := func(a int, b int) int {
return a + b
}
fmt.Println("Sum:", add(10, 20))
}
// OUTPUT: Sum: 30
Explanation:
- Anonymous functions don’t have a name.
- They can be assigned to variables and used like normal functions.
Example 5: Variadic Function
package main
import "fmt"
func sum(numbers ...int) int {
total := 0
for _, num := range numbers {
total += num
}
return total
}
func main() {
fmt.Println(sum(1, 2, 3, 4, 5))
}
// OUTPUT: 15
Explanation:
- A variadic function accepts a variable number of arguments.
- The
...intsyntax means you can pass any number of integers.
Key Notes:
- Functions can return multiple values.
- You can name return values in the function signature.
- Anonymous and variadic functions are powerful features in Go.
- Functions help achieve modular, readable, and maintainable code.