This is the 14th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Overview of functions

What is a function? In other programming languages, there are functions. A collection of functions that are encapsulated to accomplish a specific function. A function is a block of code that performs a specific task.

Definition of a function

Func funcName(inpt1 type1, inpt2 type2) (outp1 type1, outp2 type2) {return value1, value2 }Copy the code
  • Func: A function is declared beginning with func
  • FuncName: The name of a function, which together with the argument list constitutes a function signature.
  • Parametername type: list of parameters. Parameters act as placeholders. When a function is called, you can pass values to the parameters. The parameter list specifies the parameter type, order, and number of parameters. Arguments are optional, which means the function can contain no arguments.
  • Output1 type1, outpuT2 type2: Return types. The function returns a list of values. Return_types is the data type of the column values. Some functions do not require a return value, in which case return_types is not required.
  • The above return value declares two variables output1 and output2. If you don’t want to declare them, just two types.
  • If there is only one return value and no return value variable is declared, then you can omit the parentheses containing the return value (i.e. a return value may not declare a return type).
  • Function body: The set of code defined by a function.
  • The definition of the function is on the same line as the open curly brace
  • The return value more
  • Ignore function return values

Multiple return instances

Example 1.1. Define a function [actionVariables] that returns two values, adds and subtracts, and tests multiple returned values

func actionVariables(var1 int,var2 int)(int,int) {
func actionVariables(var1,var2 int)(int,int) {
    return var1 + var2,var1-var2
}
Copy the code

If the parameters of the function are the same, then the last parameter of the function can be written. The name of the return value of the function can be specified or ignored

1.2. If two or more parameters have the same type, write the type at the end

func actionVariables(var1,var2 int)(int,int) {
    return var1 + var2,var1-var2
}
Copy the code

2. Call

Var sum int var minus int sum,minus = actionVariables(1,2) FMT.Println(sum,minus)Copy the code

Example 2.1. Define a function [actionVariables2] to set named and unnamed when the function returns more than one value

func actionVariables2(var1,var2 int) (sum int,minus int) {
    sum = var1 + var2
    minus = var1 - var2
    //return sum,minus
    return
}
Copy the code

Example 2.2. Call

The sum, minus = actionVariables2 (1, 2) FMT. Println (sum, minus)Copy the code

Uncertain parameters

Variable parameters… Interface {} as a function parameter, through the analysis of go system built-in fmt.println to explain the variable parameters

/ / 1. Define function [actionVariables3], take parameters, test func actionVariables3(args... interface{}) { for _,value := range args { fmt.Println(key,value) } } // 2. Call actionVariables3(1," I am Bogo ",3.14,true)Copy the code

Mutable parameters, like slices, there are many in Go, mutable parameters are a feature of go

// Variable parameters... [actionVariables3] [actionVariables3] [actionVariables3] [actionVariables3] [actionVariables3] [actionVariables3] interface{}) { for _,value := range args { switch value.(type) { case int: fmt.Println(value,"int") case string: fmt.Println(value,"string") case float64: fmt.Println(value,"float64") case bool: fmt.Println(value,"bool") default: fmt.Println(value,"unknow") } } } //2. Call actionVariables3(1," I am Bogo ",3.14,true)Copy the code