Functions

Dart is a true object-oriented language, so even functions are objects and have a type, Function.

บรรทัดที่ 8 : การประกาศฟังก์ชัน
บรรทัดที่ 12 : การประกาศฟังก์ชัน แบบไม่ระบุชนิดของตัวแปร และชนิดของ returnt type
บรรทัดที่ 16 : การประกาศฟังก์ชันแบบ shorthand syntax
บรรทัดที่ 18 : การประกาศฟังก์ชันแบบ shorthand syntax ร่วมกับการใช้ conditional expression

Optional positional parameters
ใส่พารามิเตอร์ใน [] ทำให้กลายเป็น optional parameters

Default parameter values

กำหนด List และ Map เป็น default value

จะได้

The main() function

รันทาง command line จะไม่แสดง error ของ assert

ต้องใส่ --enable-asserts ด้วย ก็จะแสดง error ของ assert

ทีนี้ใส่พารามิเตอร์ไป 2 ตัวคือ 1 test ก็จะผ่านละ ไม่ error

Pass a function as a parameter to another function

จะได้

ลองเขียนฟังก์ช้นด้วย shorthand syntax

ฟังก์ชันซ้อนอยู่ใน ฟังก์ชันได้

Assign a function to a variable

Anonymous functions

หรือ

จะได้

ขอบเขตของตัวแปร (Lexical scope)

Lexical closures

Testing functions for equality

Return values
ปกติฟังก์ชันจะคืนค่า ถ้าไม่กำหนด return ก็จะได้คืนค่าเป็น null
ยกเว้นเรากำหนด void ไว้หน้าฟังก์ชัน ฟังก์ชันก็จะไม่คืนค่า

 

Link