YouTip LogoYouTip

Go Functions

# Go Functions Functions are basic code blocks used to perform a task. A Go program must have at least one `main()` function. You can divide different functionalities using functions; logically, each function performs a specific task. A function declaration tells the compiler the function's name, return type, and parameters. The Go standard library provides many built-in functions that can be called. For example, the `len()` function can accept different types of arguments and return the length of that type. If we pass a string, it returns the string's length; if we pass an array, it returns the number of elements in the array. * * * ## Function Definition The format for defining a function in Go is as follows: ```go func function_name( ) { function body } Function definition analysis: * `func`: A function starts with the `func` keyword. * `function_name`: The function name. The parameter list and return type constitute the function signature. * `parameter list`: The parameter list. Parameters are like placeholders; when the function is called, you pass values to the parameters. These values are called actual arguments. The parameter list specifies the parameter types, order, and count. Parameters are optional, meaning a function can also have no parameters. * `return_types`: The return type. A function can return a list of values. `return_types` is the data type of this list of values. Some functions do not need to return a value; in such cases, `return_types` is not required. * `function body`: The collection of code that defines the function. ### Example The following example is the code for a `max()` function that takes two integer parameters, `num1` and `num2`, and returns the maximum value of the two: ## Example ```go /* Function to return the maximum of two numbers */ func max(num1, num2 int) int { /* Declare local variable */ var result int if (num1 > num2) { result = num1 } else { result = num2 } return result } ## Function Call When you create a function, you define what the function needs to do. You execute the specified task by calling the function. To call a function, you pass arguments to the function, and it returns a value. For example: ## Example ```go package main import "fmt" func main() { /* Define local variables */ var a int = 100 var b int = 200 var ret int /* Call function and return the maximum value */ ret = max(a, b) fmt.Printf("Max value is : %dn", ret) } /* Function to return the maximum of two numbers */ func max(num1, num2 int) int { /* Define local variable */ var result int if (num1 > num2) { result = num1 } else { result = num2 } return result } The above example calls the `max()` function within the `main()` function. The execution result is: Max value is : 200 * * * ## Function Returning Multiple Values Go functions can return multiple values. For example: ## Example ```go package main import "fmt" func swap(x, y string) (string, string) { return y, x } func main() { a, b := swap("Google", "") fmt.Println(a, b) } The execution result of the above example is: Google * * * ## Function Parameters If a function uses parameters, those variables are called the function's formal parameters. Formal parameters are like local variables defined within the function body. When calling a function, you can pass arguments in two ways: | Passing Type | Description | | --- | --- | | (#) | Pass by value means that when calling the function, a copy of the actual argument is passed to the function. Therefore, any modifications to the parameter within the function will not affect the actual argument. | | (#) | Pass by reference means that when calling the function, the address of the actual argument is passed to the function. Therefore, any modifications to the parameter within the function will affect the actual argument. | By default, Go uses pass by value, meaning the actual arguments are not affected during the call. * * * ## Function Usage | Function Usage | Description | | --- | --- | | (#) | After a function is defined, it can be passed as an actual argument to another function. | | (#) | Closures are anonymous functions and can be used in dynamic programming. | | (#) | A method is a function that contains a receiver. |
← Go Break StatementGo For Loop β†’