Home >Backend Development >Golang >What are the requirements for golang formal parameters?

What are the requirements for golang formal parameters?

青灯夜游
青灯夜游Original
2022-12-19 11:28:424741browse

The parameters of functions in the Go language are formal parameters, which are local variables whose values ​​are provided by the parameter caller; if the types of adjacent formal parameters are the same, the types of the first few parameters can be omitted. , you only need to write the type of the last parameter, the syntax "func funcName(formal parameter 1, formal parameter 2 parameter type 1, formal parameter 3, formal parameter 4 parameter type 2, ...) (return value list) { / /Execute statement...return return value list}".

What are the requirements for golang formal parameters?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Go language function parameters

The parameters of functions in Go language are formal parameters, that is, if we pass by value , the parameter passed is actually a copy of the actual parameter, not the real actual parameter. Formal parameters are local variables whose values ​​are provided by the caller of the parameter.

In the Go language, if the types of adjacent function parameters are the same, then we can omit the types of the first few parameters and only need to write the type of the last parameter.

func funcName(param1, param2 paramType1, param3, param4 paramType2, ...)(returnVal returnType){	// 执行语句...
	return 返回值列表
}

Explanation

  • The parameters param1 and parameter param2 have the same type, then we can omit the parameter type after param1 and put it directly after param2 Write type.

  • The parameters param3 and parameter param4 have the same type, so we can omit the parameter type after param3 and write the type directly after param4.

  • returnVal returnType is the return value list, describing the variable name and type of the function return value. If the function returns an unnamed variable or no return value, the return value list The parentheses can be omitted.

Case

Use functions to find the greatest common divisor of two numbers

package main
import (
	"fmt"
)
func gcdNormal(x, y int) int {
	var n int
	if x > y {
		n = y
	} else {
		n = x
	}
	for i := n; i >= 1; i-- {
		if x%i == 0 && y%i == 0 {
			return i
		}
	}
	return 1
}
func main() {
	//用函数,实现计算两个数的最大公约数
	gcd := gcdNormal(10,20)
	fmt.Println("Gcd =", gcd)
}

After the program runs, the console output is as follows:

What are the requirements for golang formal parameters?

We define a function gcdNormal, passing in two int type parameters a and b, the function Returns a variable of type int. This function uses the exhaustive method to implement the logic of finding the greatest common divisor of parameter a and parameter b. Because the types of parameter a and parameter b are both int types, we omit the type after parameter a and write it directly in parameter b. later.

In the main() function, we called the gcdNormal function and passed in the two parameters 10 and 20. We used the gcd variable to accept the value returned by the gcdNormal function, and finally used the Println() function to print the final result.

Function parameters are formal parameters

The function parameters of the Go language are formal parameters, and modifications to the function parameters will not affect the actual parameters

package main
import (
"fmt"
)
func change(a int) {
a += 100
}
func main() {
//Go语言的函数参数是函数参数,对函数参数的修改不会影响实参
a := 10
change(a)
fmt.Println("a =", a)
}

Program After running, the console output is as follows:

What are the requirements for golang formal parameters?

We define a function change() and pass in an int type parameter a. In the function body, for The parameters of this function implement the operation of adding 100.

In the main() function, call this function and pass in the variable a. After executing the change() function, we print the value of the variable a again and find that the value of a is still 10, not 100. 110 after that.

Because in the Go language, the parameters of the function are formal parameters, that is, copies of the actual parameters, so what is modified is not the actual parameters, so the value of a will not change.

Modify actual parameters through function parameters

Go language functions must use pointer types if they want to modify actual parameters through function parameters

package main
import (
	"fmt"
)
func change(a *int) {
	*a += 100
}
func main() {
	//Go语言的函数要通过函数参数修改实参,必须要使用指针类型
	a := 10
	change(&a)
	fmt.Println("a =", a)
}

What are the requirements for golang formal parameters?

We define a function change(), passing in a parameter a of int pointer type, and in the function body, add 100 to the parameter of the function.

In the main() function, call this function and pass in the address of variable a. After executing the change() function, we print the value of variable a again and find that the value of a has been increased by 100. It became 110.

The change() function here passes in the address of a. Then we use the pointer operator in the change() function to obtain the variable pointed to by the address of a, and then modify its value. In fact, the modification is the value of variable a itself.

Summary of Go language function parameters

The parameters of functions in Go language are formal parameters, that is, if we pass by value, The parameters are actually a copy of the actual parameters, not the real actual parameters.

In the Go language, if the types of adjacent function parameters are the same, then we can omit the types of the first few parameters and only need to write the type of the last parameter.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of What are the requirements for golang formal parameters?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn