User Tools

Site Tools


tutorial:syntax

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
tutorial:syntax [2019/01/06 10:15]
admin
tutorial:syntax [2019/01/06 11:31]
admin
Line 102: Line 102:
 PROGRAM ProgramName ; PROGRAM ProgramName ;
  
-  //​Declaration scope 
 USES USES
   {include units (external .pas files)}   {include units (external .pas files)}
Line 115: Line 114:
   {Variable declarations}   {Variable declarations}
  
-{Sub-programs declarations}+{Sub-programs declarations ​& Implementations}
  
-  //​Implementation scope 
 BEGIN BEGIN
   {Executable statements (Main)}   {Executable statements (Main)}
Line 207: Line 205:
 =====Unit Structure===== =====Unit Structure=====
 บางครั้งการแยกโค๊ดออกเป็นโมดูลย่อยแล้วค่อยเรียกใช้เข้ามาในโปรแกรมหลัก จะทำให้เราสะดวกในการค้นหาและแก้ไข อีกทั้งโค๊ดส่วนนี้ยังสามารถเอาไปใช้ในหลายๆโปรแกรมในภายหน้าได้อีก เราเรียกโมดูลที่เก็บโค๊ดเหล่านี้ว่า Unit \\ บางครั้งการแยกโค๊ดออกเป็นโมดูลย่อยแล้วค่อยเรียกใช้เข้ามาในโปรแกรมหลัก จะทำให้เราสะดวกในการค้นหาและแก้ไข อีกทั้งโค๊ดส่วนนี้ยังสามารถเอาไปใช้ในหลายๆโปรแกรมในภายหน้าได้อีก เราเรียกโมดูลที่เก็บโค๊ดเหล่านี้ว่า Unit \\
-Unit คือไฟล์ .pas ที่มีส่วนประกาศหรือตัวแปรต่างๆ (uses, const, type, var) หรือโปรแกรมย่อย (functions/​procedures) อยู่ภายใน ประกอบไปด้วยส่วนที่เป็น Public และ Private โดยโครงสร้าง Unit ของ Pascal จะมีรูปแบบดังนี้+Unit คือไฟล์ .pas ที่มีส่วนประกาศหรือตัวแปรต่างๆ (uses, const, type, var) หรือโปรแกรมย่อย (functions/​procedures) อยู่ภายใน ประกอบไปด้วยส่วนที่เป็น Public ​Scope และ Private ​Scope โดยโครงสร้าง Unit ของ Pascal จะมีรูปแบบดังนี้
  
 <sxh delphi;> <sxh delphi;>
 unit unitname; unit unitname;
-  //public scope ----- +  ​ 
-   +  ​//public scope ----- 
 interface interface
   {uses, const, type, var, function, procedure ... declaration}   {uses, const, type, var, function, procedure ... declaration}
Line 222: Line 220:
 end. end.
 </​sxh>​ </​sxh>​
-\\ \\+\\ 
  
 +  *__**Public Scope**__ คือ ส่วนที่อยู่ใต้คำว่า interface ลงมา แต่อยู่เหนือคำว่า implementation ทุกครั้งที่มี Program อื่น หรือ Unit อื่น เรียกใช้ Unit ดังกล่าวนี้ (ใช้ใน uses) จะเห็น Variables หรือ Sub-program หรืออะไรก็ตามที่ประกาศไว้ในเฉพาะส่วนนี้เท่านั้น \\
 +  *__**Private Scope**__ คือ ส่วนที่อยู่ใต้คำว่า implementation เป็นส่วนที่ Program อื่น หรือ Unit อื่น ที่เรียกใช้ Unit ดังกล่าวนี้ ไม่สามารถเข้าถึงโดยตรงได้ จะเรียกใช้งานได้ก็ต่อเมื่อเรียกผ่านการ Declaration ในส่วนของ Public Scope เท่านั้น
 +
 +ที่เป็นแบบนี้เพราะ ส่วนของ implementation นั้นมักจะประกอบไปด้วย source code หลายบรรทัด แถมยังมีการเรียกใช้ Variables หรือ Sub-program ซ้ำกันเต็มไปหมด จะเป็นการยากที่จะให้ Compiler มาหา Variables หรือ Sub-program หรืออะไรก็ตาม ในสถานที่นี้ ​
 +
 +\\ \\
 =====ความรู้พื้นฐานก่อนเริ่มเขียน Pascal===== =====ความรู้พื้นฐานก่อนเริ่มเขียน Pascal=====
 ====Declaration and Implementation==== ====Declaration and Implementation====
-การเขียนโปรแกรมนอกเหนือจากต้องรู้จักโครงสร้างของโปรแกรมแล้ว ยังต้องทำความเข้าใจกับสองคำนี้ คือ Declaration กับ Implementation ซึ่งนอกจากจะเกี่ยวข้องกับการเขียน Main Program แล้ว ยังเกี่ยวข้องกับ Sub-program ด้วยนั่นเอง ​(Sub-program คือ Procedure/​Function หรือเรียกอีกอย่างว่า Method นั่นเอง เพื่อไม่ให้ดูซับซ้อนมากเกินไป ในหัวข้อนี้ จะขอเรียกพวกนี้สั้นๆว่า Sub-program แทน)\\+การเขียนโปรแกรมนอกเหนือจากต้องรู้จักโครงสร้างของโปรแกรมแล้ว ยังต้องทำความเข้าใจกับสองคำนี้ คือ Declaration กับ Implementation ซึ่งนอกจากจะเกี่ยวข้องกับการเขียน Main Program แล้ว ยังเกี่ยวข้องกับ Sub-program ด้วย (Sub-program คือ Procedure/​Function หรือเรียกอีกอย่างว่า Method นั่นเอง เพื่อไม่ให้ดูซับซ้อนมากเกินไป ในหัวข้อนี้ จะขอเรียกพวกนี้สั้นๆว่า Sub-program แทน)\\
  
  
-Declaration คือ การประกาศ ​งนึภาพตามว่า หเราจะหาว่าในโปแกมทีเราเขียนนี้ มี Method ​อะไรบ้าง เราต้งมาหาในนี้ก่อน เพราะมันหมืนการสรุปว่า ​มี Method ​อะไรบ้าง+__**Declaration**__ คือ การประกาศ ​เป็นส่วนที่ใช้บอก ​Compiler ​ว่า ​Program ​รือ Sub-program ดังล่าว ​ชืออะไร รับ Input หรือสง Output ​อะไรบ้าง เป็นต้น แต่ยั่ไดบอถึงรายละเอียดว่าทำอะไรได้บ้าง ​
  
-Implementation คือ ส่วนที่บ่งบอกว่า Program หรือ Sub-program นั้นทำอะไรบ้าง มีขั้นตอนอะไรบ้าง+__**Implementation**__ คือ ส่วนที่บ่งบอกว่า Program หรือ Sub-program นั้นทำอะไรบ้าง มีขั้นตอนอะไรบ้าง
  
 +ย้อนกลับไปดูที่โครงสร้างของ Main Program จะแบ่งแยกพื้นที่ได้ดังนี้
 +  * Declaration Scope คือ ส่วนแรก คือ บรรทัดที่ 1-16 
 +  * Implementation Scope คือ ระหว่างคำว่า BEGIN ... END. หรือ บรรทัดที่ 18-20
 +
 +<sxh delphi;​highlight:​ [1-16,​18-20]>​
 +PROGRAM ProgramName ;
 +
 +USES
 +  {include units (external .pas files)}
 +
 +CONST
 +  {Constant declarations}
 + 
 +TYPE
 +  {Type declarations}
 + 
 +VAR
 +  {Variable declarations}
 +
 +{Sub-programs declarations & Implementations}
 +
 +BEGIN
 +
 +  {Executable statements (Main)}
 +  ​
 +END.
 +</​sxh> ​
 +
 +\\
 +ทีนี้ลองมาดูที่โครงสร้างของ Sub-Program ที่อยู่ภายใน Main Program กันบ้าง ขอยกตัวอย่าง Procedure SubProg และ Function SubFunc ข้างล่างนี้ เราจะแบ่งพื้นที่ของ Sub-Program Declaration กับ Implementation ได้ดังนี้
 +  * Declaration Scope คือ ส่วนแรก ในบรรทัดที่ 6-7 (เป็นการประกาศแบบล่วงหน้า หรือ Forward Declaration ซึ่งจากตัวอย่างนี้ จะมีหรือไม่มีก็ได้)
 +  * Implementation Scope คือ บรรทัดที่ 9-17 บ่งบอกว่า Sub Program พวกนี้ ทำอะไรได้บ้าง
 +
 +<sxh delphi;​highlight:​ [6-7,​9-17]>​
 +PROGRAM ProgramName ;
 + 
 +VAR
 +  {Variable declarations}
 +
 +  program SubProg; forward;
 +  function SubFunc(inp:​string):​string;​ forward;
 +  ​
 +  program SubProg;
 +  begin
 +    writeln('​This is subProgram'​);  ​
 +  end;
 +
 +  function SubFunc(inp:​string):​string;​
 +  begin
 +    result:= inp;  ​
 +  end;
 +    ​
 +BEGIN
 +
 +  SubProg; //Call SubProg;
 +  writeln('​Call SubFunc : ',​SubFunc('​Hello!'​);​
 +    ​
 +END.
 +</​sxh> ​
 +
 +สำหรับ Unit เราสามารถแบ่งพื้นที่ได้แบบนี้
 +  * Declaration Scope คือ ส่วนแรก คือ บรรทัดที่ 1-5 
 +  * Implementation Scope คือ ระหว่างคำว่า implementation ... end. หรือ บรรทัดที่ 7-9 (สังเกตว่ามันจะคล้ายกับ BEGIN...END. ของ Main Program แค่เปลี่ยนคำแรกเป็น implementation แค่นั้นเอง)
 +<sxh delphi;​highlight:​ [1-5,​7-9]>​
 +unit unitname;
 +   
 +interface
 +  {uses, const, type, var, procedures/​functions ... declaration}
 +  ​
 +implementation
 +
 +  {Procedures/​Functions implementation here}
 +  ​
 +end.
 +</​sxh>​
 +
 +\\
 +สำหรับ Sub-Program ที่อยู่ภายใต้ Unit จะแบ่งพื้นที่ได้ดังนี้
 +  * Declaration Scope คือ ส่วนแรก ในบรรทัดที่ 6-7 (ส่วนนี้บังคับว่าต้องมี และถือเป็นการ Forward Declaration เหมือนกัน แต่ไม่ต้องใส่คำว่า forward;)
 +  * Implementation Scope คือ บรรทัดที่ 10-18 บ่งบอกว่า Sub Program พวกนี้ ทำอะไรบ้าง
 +
 +<sxh delphi;​highlight:​ [6-7,​10-18]>​
 +unit unitname;
 +   
 +interface
 +  {uses, const, type, var, procedures/​functions ... declaration}
 +  ​
 +  program SubProg;
 +  function SubFunc(inp:​string):​string;​
 +  ​
 +implementation
 +  program SubProg;
 +  begin
 +    writeln('​This is subProgram'​);  ​
 +  end;
 +
 +  function SubFunc(inp:​string):​string;​
 +  begin
 +    result:= inp;  ​
 +  end;  ​
 +end.
 +</​sxh>​
  
 +\\
 +\\
  
 ====Built-in Functions ที่ควรทราบ==== ====Built-in Functions ที่ควรทราบ====
Line 244: Line 352:
 <hidden Example-3: การใช้งาน write, writeln และ readln> <hidden Example-3: การใช้งาน write, writeln และ readln>
 **ตัวอย่าง** โปรแกรมคำนวณ Body Mass Index (BMI)\\ **ตัวอย่าง** โปรแกรมคำนวณ Body Mass Index (BMI)\\
 +__**ข้อสังเกต**__ - readln() (หรือ readln) ในบรรทัดที่ 20 นั้น ถูกใช้เพื่อเบรคหน้าจอไม่ให้ปิดตัวเองลง และผู้ใช้สามารถกด Enter เพื่อผ่านคำสั่งนี้ได้ (จบโปรแกรม)
 <sxh delphi;> <sxh delphi;>
 program BMI_Calculation;​ program BMI_Calculation;​
Line 264: Line 373:
   writeln('​Your BMI is = ',​Format('​%.2f',​[BMI]));​   writeln('​Your BMI is = ',​Format('​%.2f',​[BMI]));​
  
-  readln();+  readln(); // or readln;
 end.  end. 
 </​sxh> ​   </​sxh> ​  
tutorial/syntax.txt · Last modified: 2019/01/06 16:38 by admin