User Tools

Site Tools


tutorial:procedureandfunction

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:procedureandfunction [2019/01/04 16:02]
admin
tutorial:procedureandfunction [2019/01/04 21:11]
admin
Line 148: Line 148:
 </​sxh> ​ </​sxh> ​
  
-\\+
 <hidden Example-6: Passing Parameters (By Value, By Reference)>​ <hidden Example-6: Passing Parameters (By Value, By Reference)>​
 <sxh delphi;> <sxh delphi;>
-program ​Project1;+program ​Passing_Param;
  
 var A,​B:​integer;​ var A,​B:​integer;​
Line 182: Line 182:
   readln();   readln();
 end.  end. 
-</​sxh> ​    ​+</​sxh> ​ 
 + 
 +Compiled Results: 
 + 
 +  A = 1 
 +  B = 2 
 +  DoItA, X = 100 
 +  DoItB, X = 200 
 +  A = 1 
 +  B = 200 
 +    ​
 </​hidden>​ </​hidden>​
 +\\
 +
 +=====Forward Declaration=====
 +คือ การประกาศ Procedure/​Function ล่วงหน้าเพื่อให้ทุก Procedure/​Function สามารถเรียกใช้งานกันและกันได้หมด สาเหตุที่ต้องทำแบบนี้เพราะว่า โดยปกตินั้น Compiler จะอ่าน source code จาก บนลงล่าง ดังนั้น หากมี Procedure/​Function ตัวใดตัวหนึ่งเรียกใช้งาน Procedure/​Function ตัวอื่นที่ยังไม่ได้ประกาศ จะทำให้เกิด Error \\
 +
 +รูปแบบการประกาศสำหรับ Program จะมีคำว่า forward ต่อท้ายดังนี้
 +
 +  Procedure ProcedureName;​ forward;
 +
 +
 +<hidden Example-7: Forward Declarations on Simple Program>
 +__**อธิบาย**__ - ตัวอย่างนี้ มีการประกาศโปรแกรมย่อย ชื่อ Second ล่วงหน้า เพื่อให้โปรแกรมย่อยที่ชื่อ First ได้รู้จัก Second ก่อน ​
 +
 +<sxh delphi;>
 +program FWD_Declaration;​
 +
 +//Forward declarations
 +procedure Second; ​ forward;
 +
 +//​Implementations
 +procedure First;
 +begin
 +  writeln('​Hello First'​);​
 +  Second;
 +end;
 +
 +procedure Second;
 +begin
 +  writeln('​Hello Second'​);​
 +end;
 +
 +begin
 +  First;
 +  readln();
 +end.  ​
 +</​sxh> ​
 +
 +Compiled Results:
 +
 +  Hello First
 +  Hello Second
 +    ​
 +</​hidden>​
 +\\
tutorial/procedureandfunction.txt · Last modified: 2019/01/06 16:54 by admin