# Kotlin 學習網站: https://developer.android.com/ ## 您的第一個 Kotlin 程式 ### 定義函式 - 函式需要輸入一些內容,函示會使用這些輸入內容來達到目的。(輸入內容非必須) ![](https://i.imgur.com/xI41bqK.png) 如要將上圖轉譯為 Kotlin 程式碼,請按照下列語法或格式來定義函式。這些元素的順序十分重要。「fun」字必須排在函式名稱後面,接著是加上括號的輸入內容,最後是加上大括號的函式主體。 ![](https://i.imgur.com/ZgcXgof.png) 函式主要部分為: - 函式定義以 **fun** 開頭 - 函式名稱為 **main** - 函式沒有輸入內容,因此括號內是空白 - 函式主體 **println("Hello, world!")** 中有一行程式碼,位於函式左右大括號之間。 ![](https://i.imgur.com/bMR7tVU.png) ### 函式關鍵字 不能使用「func」、「function」或其他拼寫方式,因為 Kotlin 編譯器無法辨識您的意思。 ## 在 Kotlin 中建立及使用變數 ### 利用變數的應用程式範例 在 Google 地圖應用程式,您可以看到每個地點的詳細資料畫面,例如餐廳或商家。上方 Google 地圖應用程式螢幕截圖,顯示 Google 公司總部 (稱為 Googleplex) 的詳細資料。您覺得哪些應用程式儲存的資料是變數? - 地點的 名稱 - 地點的 星級評價 - 地點的 評論數量 - 用戶是否 曾儲存 (或加入書籤) 這地點 - 地點的 地址 - 地點的 描述 ### 資料類型 | Kotlin 的資料 | 可以儲存甚麼類型的資料 | 文字值範例 | | -------- | -------- | -------- | | String | 文字 | "Add contact" | | Int | 整個整數 | 32 | | Double | 小數 | 2.0 | | Float | 小數(不如Double精準) | 5.0f | | Boolean | true or flase | true | ### 定義和使用變數 ```kotlin= fun main() { val count: Int = 2 println(count) // 5 } ``` ```kotlin= fun main() { val count: Int = 5 print("You have $count unread messages.") } ``` ```kotlin= fun main() { val unReadCount = 10 val readCount = 100 println("You have ${unReadCount + readCount} total messages in your inbox.") } // You have 110 total messages in your inbox. ``` ### 更新變量 ```kotlin= fun main() { val cartTotal = 0 cartTotal = 20 println("Total: $carTotal") } // Val cannot be reassigned ``` 1.會發現你會將cartTotal變數初始化為0。並更新cartTotal變數為20 2.運行程式,你會收到編譯錯誤。 3.請注意,錯誤訊息表示無法再指定 val。錯誤在程式的第三列,他試圖將變數的值 cartTtoal 更改為20。當他指定初始值(0),val cartTtotal 不可以再指定給其他值 (20)。 如果需要更新變數的值,需使用 Kotlin 關鍵字 var 來宣告變數,而非 val - val 關鍵字 - 當你預期那變數不會改變時才使用 - var 關鍵字 - 當你預期那變數可能變更時使用 使用 val 時,變數為 **唯獨** 表示你只能讀取或存取變數的值,設定完後你不將無法編輯或修改他的值。 使用 var 時,變數是 **可變的**,這表示這個值是可以變更或是修改的。那個值是可變的。 ```kotlin= fun main() { var cartTotal = 0 cartTotal = 20 print("Total: $cartTotal") } // 20 ``` ### 遞增和減量運算子 ```kotlin= fun main() { var count = 10 println("You have $count unread messages.") count = count + 1 println("You have $count unread messages.") count-- print("You have $count unread messages.") } // You have 10 unread messages. // You have 11 unread messages. // You have 10 unread messages. ``` ### 字串 ```kotlin= fun main() { val nextMeeting = "Next meeting is:" val date = "January 1" val reminder = nextMeeting + date println(reminder) } // Next meeting is:January 1 ``` ```kotlin= fun main() { println("Say \"hello\"") } ``` ## 建立及使用 Kotlin 函式 ### 定義及呼叫函式 ```kotlin= fun main() { birthdayGreeting() } fun birthdayGreeting() { println("Happy Birthday, Rover!") } // Happy Birthday, Rover! ``` ### 透過函式傳回值 定義函式時,你可以指定函示要傳回的資料類型,透過再括弧後方加上冒號(:),在冒號後方加上類型名稱(Int、String 等),以指定回傳類型。 回傳敘述關鍵字 return 開頭,後面接上你希望函式輸出結果時回傳的值,例如變數。 ![](https://i.imgur.com/Pmswwn8.png) ### Unit 類型 在 Kotlin 中指定 **Unit** 傳回類型。如果函式傳回 Unit 或是不會傳回任何值,你就不必為此編寫回傳敘述。 ```kotlin= fun birthdayGreeting(): Unit { println("Happy Birthday, Rover!") println("You are now 5 years old") } ``` ### 透過 birthdayGreeting() 回傳 String 將剛剛的 Unit 回傳類型替換為 String ```kotlin= fun birthdayGreeting(): String { println("Happy Birthday, Rover!") println("You are now 5 years old!") } // A 'return' expression required in a function with a block body ('{...}') ``` 因為有設定回傳函式的傳回類型(設定為String),該函式就必須包含 **return**。 ```kotlin= fun birthdayGreeting(): String { val nameGreeting = "Happy Birthday, Rover!" val ageGreeting = "You are now 5 years old" return "$nameGreeting\n$ageGreeting" } fun main() { val greeting = birthdayGreeting() print(greeting) } // Happy Birthday, Rover! // You are now 5 years old! ``` ### 在 birthdayGreeting() 函式中新增參數 ```kotlin= fun birthdayGreeting(name: String): String { val nameGreeting = "Happy Birthday, $name" return "$nameGreeting" } fun main() { println(birthdayGreeting("Rover")) } // Happy Birthday, Rover ``` ### 包含多個參數的函式 ```kotlin= fun birthdayGreeting(name: String, age: Int): String { val nameGreeting = "Happy Birthday, $name, You are now $age years old" return "$nameGreeting" } fun main() { birthdayGreeting("Rover" , 22) } // Happy Birthday, Rover, You are now 22 years old ``` ### 具名引數 ```kotlin= fun birthdayGreeting(name: String, age: Int): String { val nameGreeting = "Happy Birthday, $name, You are now $age years old" return "$nameGreeting" } fun main() { println(birthdayGreeting(name = "Rex" , age = 2)) } // Happy Birthday, Rover, You are now 2 years old ``` ### 預設引數 ```kotlin= fun birthdayGreeting(name: String = "Rover" , age = Int): String { return "HappyBirthday, $name! You are now $age years old" } fun main() { println(birthdayGreeting(age = 5)) } // HappyBirthday, Rover! You are now 5 years old ```