Home »
Golang
Go Function Parameters and Arguments
Last Updated : July 19, 2025
In Go, functions can accept values known as parameters and use them during execution. When you call a function and provide values, those values are called arguments.
Syntax of Function Parameters
You define function parameters in the parentheses after the function name. You provide a name and a type for each parameter.
func functionName(param1 type1, param2 type2) {
// function body
}
Example: Function with Parameters and Arguments
The following example shows how to define and call a function in Go that accepts multiple parameters and prints a formatted greeting:
package main
import "fmt"
func greet(name string, age int) {
fmt.Printf("Hello %s! You are %d years old.\n", name, age)
}
func main() {
greet("Alice", 30)
greet("Bob", 25)
}
When you run the above code, the output will be:
Hello Alice! You are 30 years old.
Hello Bob! You are 25 years old.
Multiple Parameters of Same Type
If multiple parameters share the same type, you can simplify the syntax:
func add(a, b int) {
fmt.Println("Sum:", a+b)
}
Function Without Parameters
You can define a function that takes no parameters:
func sayHello() {
fmt.Println("Hello there!")
}
Passing Arguments
When calling a function, the values you pass are called arguments. They must match the type and order of the defined parameters:
sayHello() // no arguments
greet("Charlie", 40) // string and int arguments
Order Matters
The order of arguments must match the order of parameters in the function definition:
// Correct:
greet("John", 29)
// Incorrect (will cause a compile error):
greet(29, "John")
Exercise
Test your understanding of function parameters and arguments in Go:
- What are the values passed to a function called?
- What will happen if you change the order of arguments?
- Can Go functions be defined without any parameters?
Advertisement
Advertisement