Function Arguments in Golang
Last Updated : 29 Oct, 2024
In Golang, functions are groups of statements used to perform tasks, with optional returns. Go supports two main ways to pass arguments: Pass by Value and Pass by Reference. By default, Go uses pass-by-value.
Basic Terms in Parameter Passing to Functions:
- Actual Parameters: The arguments passed to a function.
- Formal Parameters: The parameters received by the function.
Example
package main
import "fmt"
// Attempts to modify the value of num
func modify(num int) {
num = 50
}
func main() {
num := 20
fmt.Printf("Before, num = %d\n", num)
modify(num)
fmt.Printf("After, num = %d\n", num)
}
In this example, num
remains unchanged after calling modify
since it’s passed by value.
Syntax
func functionName(param Type) {
// function body # Call By Value
}
func functionName(param *Type) {
// function body # Call By Reference
}
Call By Value
In call-by-value, a copy of the actual parameter’s value is passed. Changes made in the function don’t affect the original variable.
Syntax
func functionName(param Type) {
// function body
}
Example:
Go package main import "fmt" // Attempts to modify the value of num func modify(num int) { num = 50 } func main() { num := 20 fmt.Printf("Before, num = %d\n", num) modify(num) fmt.Printf("After, num = %d\n", num) }
OutputBefore, num = 20 After, num = 20
The value remains the same, as changes inside modify
don’t impact num
in main
.
Call By Reference
In call-by-reference, a pointer to the actual parameter is passed, so any changes inside the function reflect on the original variable.
Syntax
func functionName(param *Type) {
// function body
}
Example:
Go package main import "fmt" // Modifies value of num via reference func modify(num *int) { *num = 50 } func main() { num := 20 fmt.Printf("Before, num = %d\n", num) modify(&num) fmt.Printf("After, num = %d\n", num) }
OutputBefore, num = 20 After, num = 50
Since num
is passed by reference, modify
changes its value, which is reflected in main
.
Similar Reads
main and init function in Golang The Go language reserve two functions for special purpose and the functions are main() and init() function.main() functionIn Go language, the main package is a special package which is used with the programs that are executable and this package contains main() function. The main() function is a spec
2 min read
fmt.Fscan() Function in Golang With Examples In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Fscan() function in Go language scans the specified text, read from r, and then stores the successive space-separated values into successive arguments. Here newlines get counte
3 min read
fmt.Fprint() Function in Golang With Examples In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Fprint() function in Go language formats using the default formats for its operands and writes to w. Here Spaces are added between operands when any string is not used as a par
3 min read
fmt.Scan() Function in Golang With Examples In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Scan() function in Go language scans the input texts which is given in the standard input, reads from there and stores the successive space-separated values into successive arg
2 min read
fmt.Fprintf() Function in Golang With Examples In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Fprintf() function in Go language formats according to a format specifier and writes to w. Moreover, this function is defined under the fmt package. Here, you need to import th
2 min read