This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
tutorial:proceduraltype [2018/09/09 11:19] admin |
tutorial:proceduraltype [2018/10/28 12:23] admin [Function of Object] |
||
---|---|---|---|
Line 1: | Line 1: | ||
=====Procedural Type===== | =====Procedural Type===== | ||
- | ====Procedural Type คืออะไร==== | + | =====Procedural Type คืออะไร===== |
คือ การสร้างตัวแปรชนิดที่เป็น procedure/function เพื่อให้สามารถนำไปใส่ใน input Argument ของ procedure/function อันอื่นได้ หรือเรียกอีกอย่างหนึ่งคือ การทำให้ procedure/function กลายเป็นตัวแปร | คือ การสร้างตัวแปรชนิดที่เป็น procedure/function เพื่อให้สามารถนำไปใส่ใน input Argument ของ procedure/function อันอื่นได้ หรือเรียกอีกอย่างหนึ่งคือ การทำให้ procedure/function กลายเป็นตัวแปร | ||
+ | \\ \\ | ||
=====รูปแบบ===== | =====รูปแบบ===== | ||
Procedural Type มีรูปแบบดังนี้ | Procedural Type มีรูปแบบดังนี้ | ||
Line 20: | Line 20: | ||
**ตัวอย่าง** การส่ง function เข้าไปอีก function หนึ่ง \\ | **ตัวอย่าง** การส่ง function เข้าไปอีก function หนึ่ง \\ | ||
__ข้อสังเกต__: การหาค่าสูงสุดของ function โดยการสร้าง function ที่มีชื่อว่า Find_Max จะสังเกตว่าภายใน function ดังกล่าว มีการเรียก function จากภายนอกเข้ามาคำนวณ ซึ่งเรียกว่า f() ดังนั้นเราจึงต้องกำหนดให้ f() เป็นตัวแปรชนิด Procedural Type\\ | __ข้อสังเกต__: การหาค่าสูงสุดของ function โดยการสร้าง function ที่มีชื่อว่า Find_Max จะสังเกตว่าภายใน function ดังกล่าว มีการเรียก function จากภายนอกเข้ามาคำนวณ ซึ่งเรียกว่า f() ดังนั้นเราจึงต้องกำหนดให้ f() เป็นตัวแปรชนิด Procedural Type\\ | ||
- | สำหรับ Lazarus การเรียก func เข้ามาใน Find_Max ในบรรทัดที่ 30 จะเป็นการเรียกผ่าน Pointer ดังนั้นจึงต้องใส่ @ นำหน้าชื่อ function เสมอ | + | สำหรับ Lazarus การเรียก func เข้ามาใน Find_Max ในบรรทัดที่ 30 จะเป็นการเรียกผ่าน Pointer ดังนั้นจึงต้องใส่ @ นำหน้าชื่อ function เสมอ |
+ | |||
+ | {{ :introduction:plot_polynomial.png?400 |}} | ||
+ | |||
<sxh delphi;highlight: [4,13,30]> | <sxh delphi;highlight: [4,13,30]> | ||
PROGRAM Find_MaxValue_Of_Function; | PROGRAM Find_MaxValue_Of_Function; | ||
Line 56: | Line 59: | ||
</sxh> | </sxh> | ||
</hidden> | </hidden> | ||
- | ---- | + | \\ |
====ข้อแตกต่างระหว่าง Lazaus กับ Delphi ==== | ====ข้อแตกต่างระหว่าง Lazaus กับ Delphi ==== | ||
Line 102: | Line 105: | ||
---- | ---- | ||
- | ====Function of Object==== | + | =====Function of Object===== |
ตัวอย่างข้างบนนั้น ใช้ได้กับกรณีที่ function ถูกประกาศเป็น Global Scope เท่านั้น (จะสังเกตว่า func ไม่ได้อยู่ภายใต้ Class หรือ Object ใดๆ) ในกรณีที่ function ถูกประกาศเป็น Local Scope ภายใต้ Class หรือ Object ใดๆ จะต้องเปลี่ยนการประกาศ Type ใหม่เป็นดังนี้ | ตัวอย่างข้างบนนั้น ใช้ได้กับกรณีที่ function ถูกประกาศเป็น Global Scope เท่านั้น (จะสังเกตว่า func ไม่ได้อยู่ภายใต้ Class หรือ Object ใดๆ) ในกรณีที่ function ถูกประกาศเป็น Local Scope ภายใต้ Class หรือ Object ใดๆ จะต้องเปลี่ยนการประกาศ Type ใหม่เป็นดังนี้ | ||
+ | |||
+ | Type | ||
+ | TFunc = function(x:real):real of object ; | ||
<hidden Example-3: Function of Object (TForm)> | <hidden Example-3: Function of Object (TForm)> | ||
Line 173: | Line 179: | ||
</hidden> | </hidden> | ||
---- | ---- | ||
+ | =====Function is Nested===== | ||