Go語言學習筆記 —— 函數和方法問題

函數的目的
解決代碼的冗餘
不利於代碼的維護 編程

函數的定義:
爲完成某一功能的程序指令(語句)的集合稱爲函數函數

在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

相關文章
相關標籤/搜索