A function is a set of statements that execute tasks together. Each Go program has at least one function, which is generally main(), and all the most trivial programs can define additional functions.
You can put the code into independent functions. How to divide the different functions between codes, but logical division is usually to let each function perform a specific task.
The function declaration tells the compiler the name, return type, and parameters of the function related to it. A function definition provides the actual body of the function.
The Go language standard library provides a large number of built-in functions that can be called in programs. For example, the function len() requires different types of parameters and the length of the type of return value. For example, if a string is passed to it, it returns the length of the string in bytes, and if an array is passed to it, it returns the length of the array to the number of elements it has.
Functions are called methods or subroutines or programs with various names, etc.
Define a function:
The general form of function definitions in the Go programming language is as follows:
func function_name( [parameter list] ) [return_types]
{
body of the function
}
Function definitions in the Go programming language are composed of function headers and function bodies. Here are all the parts of a function:
- func starts the declaration of the function.
- Function Name: This is the actual name of the function. The function name and the parameter list together form the function signature.
- Parameters: Parameters are like a placeholder. When calling a function, you pass a parameter of a value. This value is called an actual parameter or parameter. The parameter list refers to the parameters of the function of type, order and number. Parameters are optional; that is, a function can contain any parameters.
- Return Type: A list of possible values for a function. return_types is a list of data types that the function returns. Some functions do the required operations without return values. In this case, return_type is not required.
Function Body: The function body contains a collection of statements that define function operation.
example:
The following is the source code of a function called max(). This function has two parameters num1 and num2 and returns the maximum value between the two:
/* function returning the max between two numbers */
func max(num1, num2 int) int
{
/* local variable declaration */
result int
if (num1 > num2) {
result = num1
} else {
result = num2
}
return result
}
Call a function:
To create a Go program function, you must make a definition. To use a function, the function needs to be called to perform the specified task.
When a program calls a function, program control is transferred to the called function. When a function definition is called to execute a task, the executed return statement or the closing bracket that reaches the end of its function will return to the main program.
To call a function, you only need to pass the necessary parameters and the name of the function. If the function returns a value, then the return value can be stored. For example:
package main
import "fmt"
func main() {
/* local variable definition */
var a int = 100
var b int = 200
var ret int
/* calling a function to get max value */
ret = max(a, b)
( "Max value is : %d\n", ret )
}
/* function returning the max between two numbers */
func max(num1, num2 int) int {
/* local variable declaration */
var result int
if (num1 > num2) {
result = num1
} else {
result = num2
}
return result
}
Keep the max() function and main() function and compile the source code. When running the last executable, it produces the following results:
Max value is : 200
Return multiple values from a function
Go language functions can return multiple values. For example:
package main
import "fmt"
func swap(x, y string) (string, string) {
return y, x
}
func main() {
a, b := swap("Mahesh", "Kumar")
(a, b)
}
Let's compile and run the above program, which will produce the following results:
Kumar Mahesh