“This is the 18th day of my participation in the August Gwen Challenge

function

  • Function: a block of code
  • All process control code and business code can only be written inside functions
  • Why use functions:
    • A function is put forward as a function for later maintenance, the structure is clear
  • Syntax for function declarations
    • Functions are not executed after they are declared; they must be called before they are executed
funcThe function name(Parameter list)The return value{
  / / the function body
}
Copy the code
  • Syntax for function calls
Return value := function name (parameter)Copy the code

Function with no argument and no return value

  • A function may have arguments or no arguments, and may have or have no return value
func main(a) {
	demo1()
}
func demo1(a){
	fmt.Println("Execute demo1")}// The above code is equivalent to
//func main(){
// fmt.Println(" execute demo1 ")
/ /}
Copy the code

Parametric function

  • A function can have more than one parameter, and each parameter type can be different
  • The argument represents the value that the caller wants to pass inside the function, to be used by the function.
  • The parameters to declare a function are called parameters, and the parameters to call a function are called arguments
func main(a) {
	i:=5
	s:="msr"
	show(s,i)
}
func show(name string,age int){
	fmt.Println("Name:",name,"Age",age)
}
Copy the code

There are return value functions

  • The return value of a function is the data returned to the caller for use by the caller.
  • A function that returns a value must have a return
func main(a) {
	a, b := 1.2
	fmt.Println(add(a,b))
}
func add(c, d int) int {
	return c + d
}
Copy the code
  • You can also put a variable in front of the return value type,return keyword without saying what the variable is what the return value is
func main(a) {
	a, b := 1.2
	fmt.Println(add2(a,b))
}

func add2(c, d int) (sum int) {
	sum = c + d
	return
}
Copy the code

Multiple return value functions

  • In Go, each function declaration can be defined as a multi-return function
  • All errors in the Go language are returned by return values
  • Syntax for declaring multireturn functions
funcThe function name(Parameter list) (Return value, return value){
  / / the function body
}
Copy the code
  • Syntax for calling functions
Variable, variable := function name (parameter)Copy the code
  • You can use an underscore placeholder when calling a function if you don’t want to receive it
Variable,_:= Function name (argument)Copy the code
  • Theoretically, a function can return an infinite number of values, but it is generally not necessary to define multiple returns (structs instead of multiple returns).

Code demo

  • The return value of a function may not be received, indicating that the function is executed
  • The number of variables used to receive the return value, if received, is the same as the number of returned values
  • Those that do not want to receive use the placeholder (_) placeholder
func main(a) {
	// Do not accept the function return value
	demo()

	// Each return value is received
	a, b := demo()
	fmt.Println(a, b)

	// Return values that you do not want to receive are placeholders for underscores
	c, _ := demo()
	fmt.Println(c)
}

func demo(a) (string.int) {
	return "msr".17
}
Copy the code
  • Multi-return functions can also define variables for a return value, so you don’t need to write anything after the return
func demo(a) (name string, age int) {
	name = "nsr"
	age = 17
	return
}
Copy the code

Variable parameter function

  • The Go language supports variadic functions
  • A variable parameter can be any number of parameters when a parameter is called
  • A variable parameter must be at the bottom of the argument list, with three dots added between the parameter name and type to indicate a variable parameter function
funcfunction(Parameter, parameter, name... Type){
	/ / the function body
}
Copy the code
  • The output statement is a variable parameter function, source code below
func Println(a ...interface{}) (n int, err error) {
	return Fprintln(os.Stdout, a...)
}
Copy the code
  • When declaring a function, use the variable arguments as slices in the function body

Code sample

  • Declare variable parameter declarations and calls
func main(a) {
	demo("Reading"."Write code"."Watch Tiktok video.")}func demo(hover ... string) {
	for a, b := range hover {
		fmt.Println(a, b)
	}
}
Copy the code
  • A variable argument must come after other arguments. A function cannot have more than one variable argument.
    • Because the number of common arguments is fixed, the compiler knows which argument is given to which parameter
func main(a) {
	demo("Zhang"."Reading"."Write code"."Watch Tiktok video.")}func demo(name string, hover ... string) {
	fmt.Println(name, "The hobby is")
	for a, b := range hover {
		fmt.Println(a, b)
	}
}
Copy the code

Anonymous functions

  • An anonymous function is a function without a name
  • Normal functions can be called multiple times by name, while anonymous functions (except function variables) are declared at the current location and called immediately because they have no name
  • Anonymous functions are called when they are declared, followed by the closing brace
func (a){
  
}()// The parentheses denote the call
Copy the code
  • Anonymous functions are declared inside other functions

Anonymous function demo

  • Anonymous function without arguments
  func main(a){
     func(a){
        fmt.Println("This is an anonymous function")
     }()// The parentheses denote the call
  }
Copy the code
  • There are parametric anonymous functions
func main(a) {
   func(s string) {
      fmt.Println(s, "This is an anonymous function")
   }("Pass parameters") // Pass arguments when called
}
Copy the code
  • Anonymous function with parameters and return values
func main(a) {
	r := func(s string) int {
		fmt.Println(s, "This is an anonymous function")
		return 110} ("Pass parameters") // Pass arguments when called
	fmt.Println(r)
}
Copy the code

The function variables

  • In Go, a function is also a type, and there are as many ways to write a function variable as there are different forms of the function
	var a func(a)// No parameter no return value
	var b func(int)/ / aintType parameters
	var c func(int) string/ / aintType parameter sumstringType return value
	fmt.Println(a, b, c)   
       
        
        
       
      
Copy the code
  • After a function variable is defined, it can be assigned using an anonymous function. You can also assign values using defined functions
  • The function variable definition is then syntactically identical to the normal function call, and the variable name is the function name of the normal function declaration
func main(a) {
	var a func(a)
	a = func(a) {
		fmt.Println("Execute function")}// Note that there are no parentheses here. Parentheses indicate that the function is called, and the variable a indicates that the function returns the value
	a() // Call the function

	Func b(s string){//... } * /
	b := func(s string) {
		fmt.Println("Execute the second function")
	}
	b("Parameters")

	// Use the defined function
	d := c
	d()
	// Function name c is also a variable
	c()
}

func c(a) {
	fmt.Println("C function")}Copy the code
  • Function type variables are the fifth reference type in addition to slice, map, channel, and interface
func main(a) {
	var a func(a)
	a = b
	a()
	var c func(a)
	c = a
	c()
	fmt.Printf("%p %p", a, c)// The output address is the same
}

func b(a) {
	fmt.Println("b")}Copy the code

Function as argument or return value

  • A variable can be an argument to a function or a return value type. And since functions can be treated as variables, function variables can also be treated as parameters or return values of functions
  • When a function is a parameter, the type can be written as the corresponding type
func main(a) {
	a(func(s string) {
		fmt.Println(s)
	})
}

func a(b func(s string)) {
	fmt.Println("A")
	b("What is passed to S")}Copy the code
  • Function as the return value
func main(a) {
	// Result points to the return value function.
	result := a()
	// Call the function to get the result
	fmt.Println(result())
}

func a(a) func(a) int {
	return func(a) int {
		return 110go}}Copy the code