Basic knowledge of function

Keywords: Programming Go Google

1. Definition of function The format of Go language function definition is as follows:

func function_name( parameter list ) return_types {
   //Function body
}

Function parameter Function if you use an argument, the variable can be called a parameter of the function.

Formal parameters are like local variables defined in a function body.

To call a function, you can pass parameters in two ways:

Passing refers to copying an actual parameter and passing it to the function when the function is called, so that if the parameter is modified in the function, the actual parameter will not be affected.

By default, the Go language uses value passing, which means that the actual parameters are not affected during the call. example

package main

import "fmt"

func main() {
   /* Define local variables */
   var a int = 100
   var b int = 200

   fmt.Printf("Before exchange a The value is : %d\n", a )
   fmt.Printf("Before exchange b The value is : %d\n", b )

   /* Exchange values by calling functions */
   swap(a, b)

   fmt.Printf("After exchange a Value : %d\n", a )
   fmt.Printf("After exchange b Value : %d\n", b )
}

/* Define functions that exchange values with each other */
func swap(x, y int) int {
   var temp int

   temp = x /* Save the value of x */
   x = y    /* Assign y value to x */
   y = temp /* Assign temp value to y*/

   return temp;
}

The following code execution results are:

The value of a before exchange is: 100
 The value of b before exchange is: 200
 Value of a after exchange: 100
 Value of b after exchange: 200

Reference passing refers to passing the address of the actual parameter to the function when calling the function, so the modification of the parameter in the function will affect the actual parameter. Reference passing pointer parameter passed into function

example

package main

import "fmt"

func main() {
   /* Define local variables */
   var a int = 100
   var b int= 200

   fmt.Printf("Before exchange, a Value : %d\n", a )
   fmt.Printf("Before exchange, b Value : %d\n", b )

   /* Call the swap() function
   * &a Point to a pointer, address of a variable
   * &b Pointer to b, address of b variable
   */
   swap(&a, &b)

   fmt.Printf("After exchange, a Value : %d\n", a )
   fmt.Printf("After exchange, b Value : %d\n", b )
}

func swap(x *int, y *int) {
   var temp int
   temp = *x    /* Save value on x address */
   *x = *y      /* Assign y value to x */
   *y = temp    /* Assign temp value to y */
}

The above code execution result is:

Before switching, the value of a: 100
 Before switching, the value of b: 200
 After exchange, the value of a: 200
 After exchange, the value of b: 100

The Go function can return multiple values, such as:

Example

package main

import "fmt"

func swap(x, y string) (string, string) {
   return y, x
}

func main() {
   a, b := swap("Google", "Runoob")
   fmt.Println(a, b)
}

Posted by KingOfHeart on Wed, 27 Nov 2019 06:20:45 -0800