A function is a block of operators that collectively perform a specific task. With the help of the functions, you can repeatedly call its statement block as a single unit in other parts of the program.
The function is declared as follows:
func function_name (parameter_list) (return_types){
actions
}
A function is defined by the func
keyword followed by the function name. Then there is a list of options in parentheses. After the list of parameters, the types of values returned from the function (if the function returns values) are determined. And then, in curly brackets, there are those operators, from which the function consists.
The name of a function, along with its parameter types and return values, is also called a signature.
By default, every Go program must contain at least one function, the main function, which is the input point to the application:
package main
import "fmt"
func main() {
}
func welcome() {
fmt.Println("Welcome to Golang")
}
In this case, we've defined a welcome function that doesn't take any parameters, doesn't return anything, and just prints a string to the console. You can define a function in the same file where the main function is located. But if we run this program, we don't see anything on the console. Because the program performs only those actions that are defined internally in the Main function.
The main is the entry point to the application. If we want to execute our function in the program, we need to call it in the main function:
package main
import "fmt"
func main() {
welcome()
welcome()
welcome()
}
func welcome() {
fmt.Println("Welcome to Golang")
}
To call a function, write its name, followed by values for function parameters in parentheses. But since the welcome function has no parameters in this case, After its name, write just blank parentheses. Thus, when the program is executed, the "Welcome to Golang" line will be displayed on the console three times:
Welcome to Golang
Welcome to Golang
Welcome to Golang
Through parameters, the function receives input. Parameters are indicated in parentheses after the function name. For each parameter, you specify a name and type (as for a variable). Parameters are separated from each other by commas. When you call a function, you must pass values for all of its parameters. For example, we want to use a function that adds any two numbers:
package main
import "fmt"
func main() {
multi(200, 300)
}
func multi(a int, b int) {
var result = a * b
fmt.Println("Result = ", result)
}
The multi-function has two parameters: a and b. Both parameters represent the int type, that is, integers. The function itself defines a variable that stores the multiplication of these numbers. Then the result of the numbers is displayed on the console.
In the main function, the multi-function is called. Since the function takes two parameters, it must pass values for these parameters or two arguments when it is called. Moreover, these values must correspond to the parameters by type. That is if a parameter represents an int type, then a number must be passed to it.
Values are passed by position. That is, the first value will be given to the first parameter, the second value will be given to the input parameter, and so on. As a result, we get the following console output:
Result = 60000
If several parameters in a row have the same type, then we can specify the type only for the last parameter, and the previous parameters will also represent this type:
package main
import "fmt"
func main() {
multi(200, 300, 500)
}
func multi(a, b, c int) {
var result = a * b * c
fmt.Println("Result = ", result)
}