函数的目的:
解决代码的冗余
不利于代码的维护 编程
函数的定义:
为完成某一功能的程序指令(语句)的集合称为函数函数
在go中,函数分为:自定义函数,系统函数(查看go编程手册)学习
基本语法code
func 函数名(形参列表) (返回值类型列表){ 执行语句... return 返回值列表 }
说明:
形参列表,表示函数的输入
函数中的语句,表示为了实现某一功能代码块
函数能够有返回值,也能够没有视频
package main import "fmt" func main() { // 输入两个数,再输入一个运算符(+,-,*,/),获得结果 var n1 float64 var n2 float64 var operator byte fmt.Println("res = ",res) } func cal(n1 float64,n2 float64,operator byte)float64{ var res float64 switch operator { case '+': res = n1 + n2 case '-': res = n1 - n2 case '*': res = n1 * n2 case '/': res = n1 / n2 default: fmt.Println("操做符错误...") } return res }
Go语言学习笔记来源:尚硅谷视频课程it