iT邦幫忙

2021 iThome 鐵人賽

DAY 8
0
Mobile Development

一起學Flutter,和我變Better!系列 第 8

Day 8 Dart語言-函數Function

函數
Dart是一種物件導向的語言,所以函數也是物件且為Function類型。因此Function可以是變數也可以是其他函數的參數。

(一)定義一個函數
https://ithelp.ithome.com.tw/upload/images/20210920/20140524yRGRV2pmJf.png

(二)main函數
介紹:每個應用成都必須有一個頂層的main函數,有它應用程式才能執行。main函數傳回void並具有List的可選參數。
https://ithelp.ithome.com.tw/upload/images/20210920/20140524Da8yWNbRUe.png

參數
參數有兩種類型,一種為可選,一種為必須,必須參數放在首位,後面放可選參數。

(一)可選參數
介紹:可選參數分為命名參數及位置參數,但兩種不能同時出現。

•可選命名參數Optional Named parameters
定義函數時,使用 {param1, param2, …}來指定命名參數。
https://ithelp.ithome.com.tw/upload/images/20210920/20140524zdyGw9wphi.jpg
呼叫函數時,以參數名:參數值的方式指定命名函數。
https://ithelp.ithome.com.tw/upload/images/20210920/20140524tpH1l84LLy.png
透過@required表示命名參數是必須的參數。
https://ithelp.ithome.com.tw/upload/images/20210920/201405249EKc72KbAb.png

•可選位置參數Optional positional parameters
用[ ]把一些參數標記為可選的位置參數。
https://ithelp.ithome.com.tw/upload/images/20210920/20140524rbgiLvc8hf.png

(二)預設參數
介紹:用 "=" 定義參數的預設值。
https://ithelp.ithome.com.tw/upload/images/20210920/201405246AtMcwKgWk.png

今天就介紹到這邊,明天將會介紹繼承及介面實現,敬請期待。

參考資料
橫跨Android及Apple的神話:用Dart語言神啟Flutter大業2.5節
https://ithelp.ithome.com.tw/articles/10233127
https://ithelp.ithome.com.tw/articles/10240432
https://www.jianshu.com/p/152c9c01ea9a


上一篇
Day 7 Dart語言-資料型態
下一篇
Day 9 Dart語言-繼承及介面實現
系列文
一起學Flutter,和我變Better!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言