Introduction
A function is a reusable block of code that performs a specific task. In Go, functions are declared with the func keyword and are central to organizing programs into small, testable pieces. Go also treats functions as first-class values, meaning they can be assigned to variables, passed as arguments, and returned from other functions.
Cricket analogy: A death-over specialist like Jasprit Bumrah is a reusable weapon called into any match situation; Go functions, declared with func, are similarly reusable blocks you can call repeatedly, and even pass around like naming a bowler as a variable for the next over.
Syntax
func name(param1 type1, param2 type2) returnType {
// function body
return value
}Explanation
The func keyword starts the declaration, followed by the function name, a parenthesized parameter list where each parameter has an explicit type, and an optional return type. If two or more consecutive parameters share the same type, you can omit the type from all but the last one, e.g. func add(a, b int) int. A function with no return value simply omits the return type.
Cricket analogy: Declaring a bowler's spell like func bowl(overs, speed int) wickets names the action, lists typed inputs, and states what comes back, just as Go's func syntax lists typed parameters before an optional return type.
Example
package main
import "fmt"
func add(a, b int) int {
return a + b
}
func greet(name string) {
fmt.Println("Hello,", name)
}
func main() {
sum := add(3, 4)
fmt.Println("Sum:", sum)
greet("Gopher")
var op func(int, int) int = add
fmt.Println("Via variable:", op(10, 5))
}
// Output:
// Sum: 7
// Hello, Gopher
// Via variable: 15Key Takeaways
- Functions are declared with func, a name, typed parameters, and an optional return type.
- Consecutive parameters of the same type can share a single type declaration.
- Functions are first-class values: assignable to variables, passable as arguments, and returnable from other functions.
- A function with no return type performs an action without producing a value.
Practice what you learned
1. Which keyword is used to declare a function in Go?
2. In func add(a, b int) int, what does 'a, b int' mean?
3. Which statement about Go functions is true?
4. What happens if a function's return type is omitted?
Was this page helpful?
You May Also Like
Multiple Return Values in Go
Understand how Go functions return multiple values, including the idiomatic error-handling pattern.
Variadic Functions in Go
Learn how to write and call functions that accept a variable number of arguments.
Closures in Go
Explore how Go closures capture and reference variables from their enclosing scope.
Methods in Go
Learn how to attach methods to types in Go using value and pointer receivers.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics