Go Programming: Functions - Function Syntax
Here's a breakdown of function syntax in Go, formatted in Markdown:
1. Basic Function Declaration
func functionName(parameterName parameterType, ...) (returnType1, returnType2, ...) {
// Function body - code to be executed
return returnValue1, returnValue2 // If return types are defined
}
funckeyword: Indicates the start of a function declaration.functionName: The name of the function. Follows Go's naming conventions (CamelCase).(parameterName parameterType, ...): The parameter list.parameterName: The name of the parameter.parameterType: The data type of the parameter (e.g.,int,string,bool,float64, custom types).- You can have multiple parameters separated by commas.
(returnType1, returnType2, ...): The return type list.returnType: The data type of the value returned by the function.- You can return multiple values separated by commas. If no return types are specified, the function implicitly returns
nil.
{ ... }: The function body, containing the code that will be executed when the function is called.return returnValue1, returnValue2: Thereturnstatement. Returns the specified values. The number of return values must match the number of return types declared in the function signature.
Example:
func add(x int, y int) int {
sum := x + y
return sum
}
This function add takes two integer parameters (x and y) and returns their sum as an integer.
2. Functions with No Parameters
func greet() {
fmt.Println("Hello, world!")
}
Functions can be declared without any parameters. The parentheses are still required.
3. Functions with Multiple Return Values
func divide(x int, y int) (int, int) {
quotient := x / y
remainder := x % y
return quotient, remainder
}
Go functions can return multiple values. The return types are specified in the function signature, and the return statement returns the values in the order they are declared.
Example Usage:
q, r := divide(10, 3)
fmt.Println("Quotient:", q) // Output: Quotient: 3
fmt.Println("Remainder:", r) // Output: Remainder: 1
4. Named Return Values
You can name the return values in the function signature. This can improve readability.
func divide(x int, y int) (quotient int, remainder int) {
quotient = x / y
remainder = x % y
return // Return values are implicitly returned by name
}
In this case, you can simply use return without specifying the values. Go will automatically return the values associated with the named return variables.
5. Functions with Variadic Parameters
Variadic parameters allow a function to accept a variable number of arguments of the same type.
func sum(numbers ...int) int {
total := 0
for _, number := range numbers {
total += number
}
return total
}
...int: The ellipsis (...) before the type indicates a variadic parameter.- Inside the function,
numbersis a slice of integers.
Example Usage:
fmt.Println(sum(1, 2, 3)) // Output: 6
fmt.Println(sum(10, 20, 30, 40)) // Output: 100
fmt.Println(sum()) // Output: 0 (empty slice)
6. Anonymous Functions (Function Literals)
Go supports anonymous functions, which are functions without a name. They are often used as closures.
func main() {
add := func(x int, y int) int {
return x + y
}
result := add(5, 3)
fmt.Println(result) // Output: 8
}
- The function is assigned to a variable (
addin this example). - Anonymous functions can capture variables from their surrounding scope (closures).
7. Function Types
You can define function types to create variables that hold functions.
type MathOperation func(int, int) int
func main() {
var operation MathOperation
operation = add // Assign the add function to the operation variable
result := operation(7, 2)
fmt.Println(result) // Output: 9
}
type MathOperation func(int, int) int: Defines a function type namedMathOperationthat takes two integers and returns an integer.- This allows you to pass functions as arguments to other functions, or return them as values.
These are the fundamental aspects of function syntax in Go. Understanding these concepts is crucial for writing well-structured and reusable code.