พื้นฐานปาสคาลพื้นฐาน rapidqเว็บภาษาไทยเว็บภาษาอังกฤษ

ตัวแปร โกลบอล

ลักษณะของตัวแปรที่เป็นแบบ global
1. อยู่เหนือ ส่วนประกาศ procedure, function ทั้งหมด
2. สามารถใช้ตัวแปร global ได้ทั้งในส่วนทำงาน
และส่วนประกาศ procedure, funciton































ตัวแปร โลคอล

ลักษณะของตัวแปรแบบ local
1. อยู่ภายใน ส่วนประกาศ procedure หรือ function
2. สามารถใช้ตัวแปรนั้นๆ ได้แต่ภายใน procedure หรือ function นั้นๆ
uses crt;
var
     a,b,c : byte;

function func1(x:integer) :integer;
begin
      func1 := x*b ;
end;

procedure proc1;
begin
      writeln('a=', a);
      c := 9;
      writeln('c=', c);
end;

begin
clrscr;
     a := 1;
     b :=2 ;   
               writeln('func1(a)=', func1(a));
               proc1;
     c :=3 ;
               write('c=', c);
readln;
end.
สังเกตว่า a, b, c ซึ่งถูกประกาศอยู่ด้านบน
สามารถนำมาใช้ใน function และ procedure ได้
เราจะเรียก a,b,c ว่าตัวแปรแบบ global
uses crt;

function func1(x:integer) :integer;
var
     b: byte;
begin
      b:=5;
      func1 := x*b ;
end;

procedure proc1;
var
    a: byte;
begin
      a := 3;
      writeln('a=', a);
      writeln('c=', c); {delete this line}
end;

var
    c: byte;
begin
clrscr;
     a := 1; {delete this line}
     b :=2 ;{delete this line}
     c :=3 ;
                writeln('func1(c)=', func1(c));
                proc1;
                write('c=', c);
readln;
end.
* program นี้ จะ error ครับ
ตัวแปร c ถูกประกาศอยู่ด้านล่าง
ของ proc1 จึงไม่สามารถนำ
มาใช้ในโพรซีเจอร์ proc1ได้
เพราะ proc1 ไม่รู้จักตัวแปร
ที่ถูกประกาศไว้ต่ำกว่าตัวเอง
เรียก c ว่าตัวแปร local ของส่วนทำงาน

b เป็นตัวแปร local ของ function func1
a เป็นตัวแปร local ของ procedure proc1
ดังนั้น จึงไม่สามารถนำมาใช้ในส่วนทำงาน
และใน function หรือ procedure อื่นๆ ได้

* ถ้าหากต้องการให้โปรแกรมนี้ทำงานได้
ต้องลบ แถวที่มีคำว่า {delete this line}
ออกไปให้หมดครับ

เมื่อ ตัวแปร global และ local มีชื่อซ้ำกัน
uses
       crt ;
var
       a : byte;

procedure abcdefg;
var
     a, b : byte;
begin
      a:= 5; {local indentifier}
      writeln('a (local indentifier of abcdefg)=', a);
      b:= 9; {local indentifier}
      writeln('b (local indentifier of abcdefg)=', b);
end;

begin
       clrscr ;
            a := 1; {global indentifier}
            abcdefg;
            write('a (global indentifier)=', a);
       readln ;
end.

หากชื่อของตัวแปร local กับตัวแปร global ซ้ำกัน
(ตัวแปร ชื่อ a เหมือนกัน)
การเปลี่ยนแปลงตัวแปร a ภายใน abcdefg
จะเป็นการเปลี่ยนตัวแปร local
uses
       crt ;
var
       a : byte;

procedure abcdefg;
var
     b:byte;
begin
      a:= 5; {global indentifier}
      writeln('a (global indentifier)=', a);
      b:= 9; {local indentifier}
      writeln('b (local indentifier)=', b);
end;

begin
       clrscr ;
            a := 1; {global indentifier}
            abcdefg;
            write('a (global indentifier)=', a);
       readln ;
end.

เมื่อไม่มีชื่อตัวแปร local ซึ่งซ้ำกับตัวแปร global
การเปลี่ยนแปลงตัวแปร a ภายใน abcdefg
จะเป็นการเปลี่ยนตัวแปร glocal
ส่วนการเปลี่ยนค่าตัวแปร a ในส่วนทำงาน(begin..end.) จะเป็นการเปลี่ยนแปลง ตัวแปร global ครับ

ตัวแปร global และ local มีประโยชน์อะไร?
ก็มีประโยชน์คือบางเราเขียน procedure หรือ function ขึ้นมา
แล้วสามารถตั้งชื่อซ้ำกันได้ ไม่ต้องเสียเวลาตั้งชื่อบ่อยๆ

การประกาศตัวแปรเป็น local ทำให้เรามั่นใจว่า หลังจากเราเปลี่ยนแปลงค่าของตัวแปร
ภายใน pro~หรือ func~ ไปแล้ว   ตัวแปร global ที่มีชื่อเหมือนกันจะไม่ถูกเปลี่ยนค่า

และการประกาศตัวแปรแบบ local จะช่วยทำให้โปรแกรมตรวจสอบง่าย
ต่างกับการใช้ตัวแปร global มาดำเนินการใน หลายๆ procedure หรือหลายๆ function
บางกรณี เมื่อต้องการตรวจสอบความถูกต้องของการทำงานจะต้องตรวจสอบหลายจุด

แต่การใช้ตัวแปร local มากๆ มีข้อเสียคือ จะทำให้กินหน่วยความจำเพิ่มขึ้น
ปกติหน่วยความจำมีมากพอ แต่บางโปรแกรมกินหน่วยจำมาก
ตัวแปรไหนใช้แทนกันได้ ก็ควรจะใช้เป็นแบบ global เพราะการใช้ตัวแปรเดียว
ในหลายๆ ส่วน จะช่วยประหยัดหน่วยความจำได้ระดับหนึ่ง










การส่งผ่านพารามิเตอร์

   คือการแทนค่าให้กับ ตัวแปรที่ทำหน้าที่เป็น parameter  
   ซึ่งอยู่ใน procedure หรือ function
   เพื่อนำค่านั้นไปดำเนินการกับคำสั่งต่างๆ ภายใน
    procedure หรือ function นั้นๆ

1. Passing Value
การส่งแบบ Value นั้น
เป็นการส่งที่ส่งเพียง "ค่า" หรือ "ค่าของตัวแปร"
ไปยังตัวแปรที่มีหน้าที่เป็นพารามิเตอร์

กรณีที่ส่งค่าโดยใช้ "ค่าของตัวแปร"
จะไม่มีการเปลี่ยนแปลงค่าของตัวแปรนั้นๆ
ภายในการทำงานของ procedure หรือ function

ตัวอย่าง การส่งค่าแบบ value ...
uses
       crt ;
var
       doraemon: string;

procedure nobita (magicitem:string);
begin
   writeln( 'the magicitem is ', magicitem);
   magicitem := 'copter maipai';
   { paremeter is changed }
end;

begin
       clrscr ;
          doraemon := 'time machine';
          nobita ( doraemon );
          nobita ( doraemon );
       readln ;
end.
ในโปรแกรมนี้กำหนด parameter
ให้รับการส่งผ่านแบบ value
ดังนั้นแม้จะมีการเปลี่ยนค่าของ parameter
ค่าของ argument ก็ไม่มีการเปลี่ยนแปลง

   :=: ในที่นี้ parameter คือ magicitem :=:
   :=: ในที่นี้ argument คือ doraemon :=:

จึงแสดงผลดังนี้

the magicitem is time machine
the magicitem is time machine
เกมทดสอบความจำ ก - ฮ
2. Passing By Reference
การส่งค่าแบบ By Reference นั้น
เป็นการให้ตัวแปรที่เป็นตัวส่งมาดำเนินการกับคำสั่งต่างๆ
ภายใน procedure หรือ function แทนตัวแปรที่มีหน้าที่
เป็นพารามิเตอร์ใน procedure หรือ function นั้นๆ

หากมีการเปลี่ยนแปลงค่าของพารามิเตอร์ใน
procedure หรือ function ค่าของตัวแปร
ที่เป็นตัวส่งก็จะเปลี่ยนไปด้วย

ตัวอย่าง การส่งค่าแบบ By Reference ...
uses
       crt ;
var
       doraemon: string;

procedure nobita (var magicitem:string);
begin
   writeln( 'the magicitem is ', magicitem);
   magicitem := 'copter maipai';
   { paremeter is changed }
end;

begin
       clrscr ;
          doraemon := 'time machine';
          nobita ( doraemon ); 
          {ตอนนี้ doraemon เท่ากับ copter maipai}
          nobita ( doraemon ); 
       readln ; 
end.
ในโปรแกรมนี้กำหนด parameter
ให้รับการส่งผ่านแบบ By Reference
เมื่อมีการเปลี่ยนแปลงค่า parameter
ค่าของ argument ก็จะเปลี่ยนไปด้วย

   :=: ในที่นี้ parameter คือ magicitem :=:
   :=: ในที่นี้ argument คือ doraemon :=:

จึงแสดงผลดังนี้

the magicitem is time machine
the magicitem is time copter maipai
best view with ie 5.0++, screen 600x800