บทที่ 05 ฟังก์ชัน (function)

23
ศิริชัย นามบุรี ฟงกชัน(Functions) บทที่ 5 ฟงกชัน (Function) ความหมายและประโยชนของฟงกชัน ฟงกชัน (Function) หมายถึง ประโยคคํ าสั่ง(statements) ชุดหนึ่งซึ่งมีชื่อเรียกใชโดยเฉพาะ ฟงกชันหนึ่ง จะทํ าหนาที่เฉพาะอยางใดอยางหนึ่ง สวนอื ่น ของโปรแกรมสามารถเรียกสเตตเมนต ชุดนี ้ได โดยการเรียกใช ชื่อฟงกชัน นั ้น ใหถูกตองตามรูปแบบที่ฟงกชันนั้น กําหนดไว ฟงกชันในภาษา C++ มี 2 ประเภท คือ 1. User Defined Function คือ ฟงกชันที ่ผู เขียนโปรแกรมสรางขึ ้นหรือกํ าหนดขึ ้นเอง ตามรูป แบบการสรางฟงกชันของ C++ เพื ่อนํ ามาใชในการเขียนโปรแกรมของตนเอง (จะกลาวถึงรายละเอียด ตอไป) 2. Standard Function คือ ฟงกชันมาตรฐานที่บริษัทผูสราง Compiler ภาษา C++ ไดสราง รวบรวมไวในคลัง (Library) ผู เขียนโปรแกรมสามารถเรียกใชไดทันที ไดแก ฟงกชันที่ประกอบอยูใน header file ตาง ขณะเรียกใช ตอง #include ชื่อไฟลที่รวบรวมฟงกชันนั้นไวกอน เชน ฟงกชัน clrscr() ทํ าหนาที่ลางจอภาพใหวางและเลื่อน cursor ไปไวที่มุมซายบนของจอภาพ เปนฟงกชันอยูใน ไฟล conio.h เปนตน ประโยชนฟงกชันในการเขียนโปรแกรมใน C++ มีดังตอไปนี้ 1. ชวยใหไมตองเขียน statement เดิม ซํ้ ากันหลายครั้งในโปรแกรมเดียวกัน 2. ชวยใหสามารถคนหาสวนที ่ผลหรือสวนที ่ตองปรับปรุงไดอยางรวดเร็ว เนื ่องจากเราทราบวา แตละฟงกชันทํ าหนาที่อะไร และสรางฟงกชันไวที่สวนใดของโปรแกรม 3. ทํ าใหโปรแกรมมีขนาดกระทัดรัด ทํ าความเขาใจไดงายและรวดเร็ว เพราะเขียนแบงเปน ฟงกชันตามงานหรือหนาที ่ที ่ตองการเขียน 4. เนื่องจากฟงกชันมีการทํ างานเปนอิสระ สามารถนํ าฟงกชันที่สรางไวและมีการทํ างานเปน มาตรฐานแลว เก็บไวใน Header File เพื่อใหเปนคลังคํ าสั่ง(Library) ของผูใช นํ าไปใชไดในโปรแกรม อื ่น ไดอีก ซึ ่งชวยใหเราเขียนโปรแกรมใหมไดรวดเร็วขึ ้น โดยการนํ าฟงกชันที่มีอยูแลวมาใชใหมได

Upload: phichya-laemluang

Post on 27-Jul-2015

1.552 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

บทที่ 5ฟงกชัน (Function)

♦ !ความหมายและประโยชนของฟงกชันฟงกชัน (Function) หมายถึง ประโยคค ําสั่ง(statements) ชุดหนึ่งซึ่งมีชื่อเรียกใชโดยเฉพาะ

ฟงกชันหนึ่ง ๆ จะท ําหนาที่เฉพาะอยางใดอยางหนึ่ง สวนอ่ืน ๆ ของโปรแกรมสามารถเรียกสเตตเมนตชุดน้ีได โดยการเรียกใช ชื่อฟงกชัน น้ัน ๆ ใหถูกตองตามรูปแบบที่ฟงกชันนั้น ๆ กํ าหนดไว

ฟงกชันในภาษา C++ มี 2 ประเภท คือ1. User Defined Function คือ ฟงกชันท่ีผูเขียนโปรแกรมสรางข้ึนหรือกํ าหนดข้ึนเอง ตามรูป

แบบการสรางฟงกชันของ C++ เพ่ือนํ ามาใชในการเขียนโปรแกรมของตนเอง (จะกลาวถึงรายละเอียดตอไป)

2. Standard Function คือ ฟงกชันมาตรฐานที่บริษัทผูสราง Compiler ภาษา C++ ไดสรางรวบรวมไวในคลัง (Library) ผูเขียนโปรแกรมสามารถเรียกใชไดทันที ไดแก ฟงกชันที่ประกอบอยูในheader file ตาง ๆ ขณะเรียกใช ตอง #include ชื่อไฟลที่รวบรวมฟงกชันนั้นไวกอน เชน ฟงกชันclrscr() ท ําหนาที่ลางจอภาพใหวางและเลื่อน cursor ไปไวที่มุมซายบนของจอภาพ เปนฟงกชันอยูในไฟล conio.h เปนตน

ประโยชนฟงกชันในการเขียนโปรแกรมใน C++ มีดังตอไปนี้1. ชวยใหไมตองเขียน statement เดิม ๆ ซํ ้ากันหลายครั้งในโปรแกรมเดียวกัน2. ชวยใหสามารถคนหาสวนท่ีผลหรือสวนท่ีตองปรับปรุงไดอยางรวดเร็ว เน่ืองจากเราทราบวา

แตละฟงกชันท ําหนาที่อะไร และสรางฟงกชันไวที่สวนใดของโปรแกรม3. ท ําใหโปรแกรมมีขนาดกระทดัรัด ท ําความเขาใจไดงายและรวดเร็ว เพราะเขียนแบงเปน

ฟงกชันตามงานหรือหนาท่ีท่ีตองการเขียน4. เนื่องจากฟงกชันมีการท ํางานเปนอิสระ สามารถนํ าฟงกชันที่สรางไวและมีการท ํางานเปน

มาตรฐานแลว เก็บไวใน Header File เพื่อใหเปนคลังคํ าสั่ง(Library) ของผูใช นํ าไปใชไดในโปรแกรมอ่ืน ๆ ไดอีก ซ่ึงชวยใหเราเขียนโปรแกรมใหมไดรวดเร็วข้ึน โดยการนํ าฟงกชันที่มีอยูแลวมาใชใหมได

Page 2: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

59

♦ !สวนประกอบในการสรางฟงกชันฟงกชันท่ีเราสรางข้ึนเอง เรียกวา User Defined Function ซึ่งใน C++ กํ าหนดใหเม่ือมีการเรียก

ใชชื่อฟงกชันใด ๆ จะตองมีการ สงคากลับมา (return value) ในชื่อฟงกชันนั้นเสมอ มีรูปแบบโครงสรางของฟงกชันประกอบไปดวยสวนหัวของฟงกชันและสวนของ statement ดังน้ี

function_type function_name (parameter1,parameter2,…) //function header{ int … //declaration variable in function

float … //declaration variable in function statement; //statement in function statement; //statement in function

return(value); //function must return only one or null value to function_name }

ความหมายของสวนประกอบในฟงกชัน มีดังนี้function_type คือ ประเภทคาหรือขอมูลท่ีไดจากการทํ างานของฟงกชัน ซึ่งจะตองใหคาคืน

กลับมาเก็บไวในชื่อของฟงกชัน (function_name) ถาเปนประเภท void จะเปนฟงกชันประเภทที่ตองมีการ return value

function_name คือ ชื่อฟงกชันที่กํ าหนดข้ึนตามกฎเกณฑการต้ังช่ือของ C++ และจะเปนชื่อที่ใชส ําหรับการเรียกใชฟงกชันตอไป

(parameter1,parameter2,…) หรือพารามิเตอรประกอบไปดวย ประเภทและชื่อของตัวแปรใชรับคาคงท่ีเพ่ือนํ ามาใชท ํางานในฟงกชัน ในขณะที่ฟงกชันนั้นถูกเรียกใชท ํางาน พารามิเตอรของฟงกชันมีมากกวา 1 ตัวและหลายประเภทได

return(value); โดยที ่return เปนคียเวิรด และ value คือคาคงที่ที่สงคืนไปใหแกชื่อฟงกชัน 1คาหรือไมมีก็ได ( กรณีเปนฟงกชันที่ไมใหคาใด ๆ)

ตัวอยางการกํ าหนดชื่อฟงกชัน เชน

Page 3: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

60

int factorial(int number) parameter ของฟงกชันเปน integerชื่อฟงกชัน factorial

ประเภทฟงกชันใหคาเปน integer

ความหมาย คือ ฟงกชันชื่อ factorial() คาที่ไดจากการทํ างานของฟงกชันเปน int และขณะเรียกใชตองมีการรับคาพารามิเตอรมาเก็บไวท่ีตัวแปร number และเปนประเภท int เพื่อในมาใชภายในฟงกชัน

void line()ความหมายคือ ฟงกชันชื่อ line() เปนฟงกชันท่ีไมมีพารามิเตอร เมื่อฟงกชันท ํางาน

เสร็จแลว จะไมใหคาใด ๆ คืนกลับมา เพราะมปีระเภทเปน void

float power(int base, int exp)ความหมาย ฟงกชันชื่อ power() คาที่ไดจากฟงกชันเปน float และขณะเรียกใชตองมี

การรับคาพารามิเตอร 2 ตัว ตัวแรก base เปน int และตัวที่สอง exp เปน int มาใชในฟงกชันดวย

♦ !การสรางและการเรียกใชฟงกชันในโปรแกรม

การสรางและการเรียกใชฟงกชันในโปรแกรม มีการกํ าหนดรูปแบบการสรางและเรียกใช ดังน้ี

Page 4: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

61

#include <header file>#include <header file>//declaration 2 prototype function in this program are line() and factorial()void line();int factorial(int number);

void main() //function main(){ int … //declaration variable in function main()

statement; //statement in function factorial(int value or variable); //call function factorial() in function main()

statement; //statement in function line(); //call function line() in function main() statement; //statement in function

} //end of main() and end of this program

void line() { int … //declaration variable in function float … //declaration variable in function

statement; //statement in function statement; //statement in function } //return

int factorial(int number) { int … //declaration variable in function factorial()

float … //declaration variable in function factorial() statement; //statement in function factorial()

line(); //call function line() in function factorial() statement; //statement in function factorial() return(value);//return only one value to factorial() function

} //return

Page 5: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

62

จากโครงสรางโปรแกรมท่ีมีการสรางฟงกชัน สรุปไดดังนี้1. การประกาศชื่อฟงกชันที่ผูใชก ําหนดไวท่ีตอนตนโปรแกรมกอนฟงกชัน main() เรียกวา

ฟงกชันตนแบบ (prototype) หรือการกํ าหนดฟงกชัน (function declaration) แสดงวาในโปรแกรมมีการสรางและเรียกใชฟงกชันเหลานี้ ซึ่งสามารถเรียกใชชื่อฟงกชันนั้น ณ ตํ าแหนงใด ๆ ของโปรแกรมก็ไดยกเวนหาม เรียกใชช่ือฟงกชันน้ันในตัวฟงกชันเอง การประกาศฟงกชันตนแบบ(prototype) ที่มีพารามิเตอร(parameters) ท ําได 2 ลักษณะ คือ

- ในสวนของพารามิเตอรของฟงกชัน เขียนท้ังชนิดขอมูลและตัวพารามิเตอร เชนint factorial(int number);

- ในสวนของพารามิเตอรของฟงกชัน เขียนเฉพาะชนิดขอมูล เชนint factorial(int);

2. การเรียกใชฟงกชันมีรูปแบบการเรียกใชเสมือนเปน statement เชน line() factorial(x) โดยที่ฟงกชันใดที่มีพารามิเตอรก ําหนดไว ตองใสคา argument ใหถูกตองในขณะเรียกใชดวย และเรียก x ของฟงกชัน factorial() วาอากิวเมนต (argument)

3. เมื่อมีการเรียกใชฟงกชัน ณ ตํ าแหนงใด โปรแกรมจะไปทํ างานในฟงกชันนั้นจนเสร็จ แลวจะกลับมาทํ างานตอใน statement ถัดไปจากตํ าแหนงที่เรียกใชฟงกชัน

4. สามารถเรียกใชฟงกชันกี่ครั้งก็ไดในโปรแกรม

♦ !ฟงกชันไมมีพารามิเตอร ฟงกชันไมมีพารามิเตอร หมายถึง ฟงกชันที่ไมมีการรับคาพารามิเตอรมาใชในขณะท ํางาน

และการเรียกใชก็ไมตองสง argument มาใหฟงกชัน อาจเปนฟงกชันประเภทคืนคา (return value) หรือไมมีการคืนคาใด ๆ ใหแกชื่อฟงกชันเมื่อท ํางานเสร็จก็ได

• ! ตัวอยางโปรแกรม line_non.cpp แสดงการสรางและเรียกใชฟงกชันแบบไมมีพารามิเตอรและไมมีการคืนคาใด ๆ คือ ฟงกชันประเภท viod

/*Program : line_non.cpp Process : creat non-parameter function line() */#include <iostream.h>#include <conio.h>

//declaration prototype functionvoid line();//begin main programvoid main(){ int i;

Page 6: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

63

clrscr(); cout<< "Display 1 line from line() function\n"; line(); //call line() and non-argument getch(); cout<< "Display 5 lines from line() function\n"; for (i=1;i<=5;++i) line(); //call line() and non-argument getch();}//end main program

void line() //non-parameter function{ cout<< "_________________________________\n";}

• ! ตัวอยางโปรแกรม func_non.cpp แสดงโปรแกรมที่มีการสรางฟงกชันประเภทที่ไมมีparameter ไมมีการ return value คือ ฟงกชัน input() และฟงกชันที่ไมม ีparameterแตมีการ return value คือฟงกชัน summation()

/*Program : func_non.cpp process : display return value and non-parameter function*/#include <iostream.h>#include <conio.h>//prototype function declarationint summation();void input();//global variableint x,y;

void main(){ clrscr(); input(); //called function input() cout<<"Result of x+ y =\a"<<summation(); //called function summation() getch();}

void input() //non-return value function{ cout<<"Enter 2 number for summation : "<<endl<<endl; cout<<"Number1 : ";cin>>x;

Page 7: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

64

cout<<"Number2 : ";cin>>y;}

int summation() //return value function{ int result; result=x+y; return (result); //return result value to summation() called}

♦ !ฟงกชันมีพารามิเตอรฟงกชันมีพารามิเตอร หมายถึงฟงกชันที่มีการรับคาพารามิเตอรมาใชในขณะฟงกชันท ํางาน ดัง

นั้นการเรียกใชฟงกชันประเภทนี ้ ตองสง argument มาใหฟงกชันดวย จึงจะสามารถท ํางานได ดังตัวอยาง

• ! ตัวอยางโปรแกรม line_par.cpp แสดงการสรางและเรียกใชฟงกชันแบบมีพารามิเตอรและไมมีการคืนคาใด ๆ (เปนฟงกชันประเภท void) ใหแกชื่อฟงกชัน ณ จุดเรียกใช คาที่ไดจากการทํ างานของฟงกชัน จะตองนํ าไปใชอยางใดอยางหน่ึงในโปรแกรม เชน แสดงคานั้นทางจอภาพ เก็บคาที่ไดไวในตัวแปร เปนตน

/*Program : line_par.cpp Process : creat parameter function line()*/#include <iostream.h>#include <conio.h>void line(int amount); //declaration prototype function//begin main programvoid main(){ int i=45; //begin statement clrscr(); cout<< "Display line from line() function\n"; line(30); //call line() and send constant argument is 30 getch(); line(50); //call line() and send constant argument is 50 getch(); line(i); //call line() and send variable argument is i getch();}//end main programvoid line(int amount) //amount is int parameter of function{ int x;

Page 8: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

65

for(x=1; x<=amount;++x) cout<<"_"; cout<<'\n';}

หมายเหตุ จากโปรแกรมตัวอยาง การก ําหนดคาอารกิวเมนต (argument) ในการเรียกใชฟงกชันท ําได 2 ลักษณะ คือ

1. กํ าหนดเปนคาคงท่ี (constant) ใหเปนประเภทเดียวกันกับพารามิเตอรของฟงกชัน เชน การเรียกใชฟงกชัน line(30) line(50) ซึ่ง 30 และ 50 คือ คาคงที่ชนิด int

2. กํ าหนดเปนตัวแปร(variable) โดยกํ าหนดใหเปนตัวแปรประเภทเดียวกันกับพารามิเตอรของฟงกชัน เชน การเรียกใชฟงกชัน line(i) ซึ่ง i เปนตัวแปรชนิด int

• ! ตัวอยางโปรแกรม func_par.cpp แสดงการคํ านวณการบวก ลบ คูณ หาร เลขจ ํานวนจริง 2 จ ํานวน โดยการสรางฟงกชันประเภทที่มี parameters และมีการreturn value ใหแกช่ือฟงกชัน ณ ตํ าแหนงท่ีเรียกใชฟงกชัน

/*Program : func_par.cpp process : display return value and parameter function calculate addition,subtract,multiply and divide*/#include <iostream.h>#include <conio.h>#include <iomanip.h>//prototype function declarationvoid input();float addition(float x, float y);float subtract(float x, float y);float multiply(float x, float y);float divide(float x, float y);//global variablefloat num1,num2;

void main(){ clrscr(); input(); cout<<"Result of addition = "<<addition(num1,num2)<<endl; cout<<"Result of subtract = "<<subtract(num1,num2)<<endl; cout<<"Result of multiply = "<<multiply(num1,num2)<<endl; cout<<"Result of subtract = "<<divide(num1,num2)<<endl<<endl; cout<<"Calculate by sent argument to parameters of function..."<<endl;

Page 9: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

66

cout<<"Result of 10.0+20.25= "<<addition(120.0,20.25)<<endl; cout<<"Result of 10.0-20.25= "<<subtract(120.0,20.25)<<endl; cout<<"Result of 10.0*20.25= "<<multiply(120.0,20.25)<<endl; cout<<"Result of 10.0/20.25= "<<setprecision(3)<<divide(10.0,20.25)<<endl; getch();}

void input() //non-parameter and non-return value function{ cout<<"Enter 2 number for calculate : "<<endl<<endl; cout<<"Number1 : ";cin>>num1; cout<<"Number2 : ";cin>>num2;}

float addition(float x, float y){ float result; result=x+y;result=x-y; return (result);}

float subtract(float x, float y){ float result; result=x-y; return (result);}

float multiply(float x, float y){ float result; result=x*y; return (result);}

float divide(float x, float y){ float result; result=x/y; return (result);}

Page 10: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

67

♦ !ฟงกชันแบบไมประกาศ prototypeการสรางฟงกชันในโปรแกรมแบบไมประกาศเปน prototype คือ การสรางรายะเอียดของ

ฟงกชันตางๆ ไวกอนฟงกชัน main() การเรียกใชฟงกชันประเภทนี้จะตองเรียกใชตามล ําดับของการสรางฟงกชันในโปรแกรม ฟงกชันที่ถูกเรียกใชจะตองสรางอยูกอนฟงกชันที่เรียกใชเสมอ เหมาะส ําหรับการเขียนโปรแกรมงาย ๆ หรือมีขนาดส้ัน ๆ ไมสลับซับซอนมากนัก

• ! ตัวอยางโปรแกรม non_prot.cpp แสดงการสรางฟงกชนัแบบไมประกาศ prototype ไวกอนดังรายละเอียดโปรแกรมในหนาตอไป

/*Program : non_prot.cpp Process : creat non prototype function */#include <iostream.h>#include <conio.h>

// create non-prototype function before function main()void line1(int amount) //parameter function{ int x; for(x=1; x<=amount;++x) cout<<"_"; cout<<'\n';}

void line2() //non-parameter function{ cout<< "*********************************\n"; line1(65);}//begin main programvoid main(){ clrscr(); cout<< "Display line from line1() function\n"; line1(40); //call line1() and constant argument is 40 getch(); cout<< "\nDisplay line from line2() function\n"; line2(); //call line2() and constant argument is 50 getch();} //end main program

Page 11: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

68

♦ !อินไลนฟงกชัน (inline function)โปรแกรมที่เรียกใชฟงกชันจะมีการเรียกใชหนวยความจ ํานอยกวาโปรแกรมที่ไมเรียกใชฟงกชัน

ทั้งนี้เพราะฟงกชันจะจองและใชพื้นที่หนวยความจ ําขณะถูกเรียกใชเทานั้น เมื่อฟงกชันท ํางานเสร็จจะคืนพื้นที่หนวยความจํ าใหกับโปรแกรม สวนโปรแกรมที่ไมเรียกใชฟงกชันจะมีการใชหนวยความจํ าเต็มท่ีตลอดเวลาตามความตองการของโปรแกรมในขณะทํ างาน

โปรแกรมที่เรียกใชฟงกชันมีสวนดีในแงประหยัดการใชหนวยความจํ า แตการเรียกใชฟงกชันท ําใหโปรแกรมตองใชเวลาเพ่ิม มีผลท ําใหการท ํางานของโปรแกรมชากวาโปรแกรมที่ไมเรียกใชฟงกชันแตเราสามารถกํ าหนดใหฟงกชันในโปรแกรมถูกเรียกใชโดยจองพื้นที่หนวยความจํ าตลอดเวลา ไมเสียเวลาในการเรียกใช โดยกํ าหนดใหฟงกชันเปนประเภท อินไลนฟงกชัน (inline function)

วิธีการสราง อินไลนฟงกชัน (inline function) มีวิธีการดังนี้1. สรางอินไลนฟงกชันอยูกอนฟงกชัน main()2. เพ่ิมคียเวิรดคํ าวา inline ที่สวนหัวของฟงกชันหมายเหตุ ขอจํ ากัดของอินไลนฟงกชัน คือ ฟงกชันตองมีขนาดสั้น ถาอินไลนฟงกชันมีขนาด

ยาวเกินไป compiler ของ C++ จะ compile ใหเปนฟงกชันที่มีการเรียกใชแบบปกติ• ! ตัวอยางโปรแกรม inline.cpp แสดงการสรางและการเรียกใช อินไลนฟงกชัน ชื่อ square()

ซ่ึงทํ าหนาท่ีค ํานวณหาคาเลขยกก ําลังสอง ดังนี้

/*Program : inline.cpp Process : creat and call inline function*/

#include<iostream.h> #include<conio.h>

inline float square(float number) //declaration inline function { return(number*number); }

void main() //begin function main() { float x; clrscr(); cout<< "Enter number to calculate square : "; cin>>x; cout<< "Result of square = "<<square(x); //call square() function getch(); }

Page 12: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

69

♦ !รูปแบบการสงอารกิวเมนตใหฟงกชัน (reference argument)ฟงกชันประเภทท่ีสรางแบบมีพารามิเตอร(parameter) เมื่อถูกเรียกใชจะตองมีการสงอารกิว

เมนต (argument) ไปใหแกฟงกชันนั้น ๆ โดยท่ีชนิดของตัวแปรหรือคาคงท่ีท่ีของอารกิวเมนตจะตองเปนชนิดเดียวกับพารามิเตอรของฟงกชันและมีจํ านวนเทากับพารามิเตอร และกรณีท่ีมีพารามิเตอรมากกวา 1 ตัว ลํ าดับการสงอารกิวเมนตตองตรงกับลํ าดับของพารามิเตอรท่ีกํ าหนดไว ดังตัวอยาง เชน

void main(){ int x = 5, y=2; float result; result = power(x, y); //เรียกใชฟงกชัน power() ตองสง argument x และ y ไปใหฟงกชัน} โดยที ่ x และ y เปนตัวแปรประเภท int เหมือนกับตัว parameters

float power(int base, int exp) //ตัวแปร base , exp เปน parameters ของ function{ //รอรับคาจาก argument x และ y ตามล ําดับ statement1; statement2; return value;}

ลักษณะของการสงอารกิวเมนตที่เปน ตัวแปร ใหแกฟงกชัน มี 2 ลักษณะ คือ1. สงเฉพาะคาอารกิวเมนต (passed argument by value) หมายถึง เม่ือสงคาของตัวแปรท่ีเปน

อาร็กิวเมนตไปใหแกพารามิเตอรของฟงกชันแลว เมื่อฟงกชันท ํางานเสร็จแลว คาตัวแปรท่ีเปนอารกิวเมนตยังคงมีคาเดิม การท ํางานของฟงกชันไมมีผลท ําใหตัวแปรอารกิวเมนตเปล่ียนแปลงคา เรียกอารกิวเมนตประเภทน้ีวา value argument

2. สงและเปลี่ยนคาอารกิวเมนต(passed argument by reference) เม่ือสงคาของตัวแปรท่ีเปนอาร็กิวเมนตไปใหแกพารามิเตอรของฟงกชันแลว เมื่อฟงกชันท ํางานเสร็จแลว คาตัวแปรท่ีเปนอารกิวเมนตมีการเปลี่ยนแปลง การท ํางานของฟงกชันท ําใหตัวแปรอารกิวเมนตเปล่ียนแปลงคาไปจากเดิมเรียกอารกิวเมนตชนิดน้ีวา reference argument

Page 13: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

70

รูปแบบการก ําหนดและการเรียกใชฟงกชันที่มีการสงอารกิวเมนตแบบ value argument และreference argument มีดังน้ี

#include <header file>//declaration prototype functionfloat square(float number);float area(float& width, float& high);

void main(){ float x,y,z; statement; }float square(float number) // value argument function{

statement; return value_of_function;}float area(float& width, float& high) // reference argument function{ statement; return value_of_function;}

หมายเหตุ การก ําหนดฟงกชันใหเปนประเภท reference argument ใชเคร่ืองหมาย & ตอทายชนิดขอมูลของพารามิเตอรในการกํ าหนดสวนหัวของฟงกชัน ในฟงกชันสามารถก ําหนดท้ัง referenceargument และ value argument รวมกันได

Page 14: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

71

• ! ตัวอยางโปรแกรม argument.cpp แสดงการเปรียบเทียบการเรียกใชฟงกชันแบบ valueargument และ แบบ reference argument

/*Program : argument.cpp Process : show creat and call value and reference function*/

#include <iostream.h> #include <conio.h> //declaration prototype function float sum(float arg1,float arg2); float multiply(float& arg3,float& arg4);

void main() //begin main program { float arg1,arg2,arg3,arg4; clrscr(); cout<< "Please enter 4 number for argument : \n"; cout<< "Argument1 : ";cin>>arg1; cout<< "Argument2 : ";cin>>arg2; cout<< "Argument3 : ";cin>>arg3; cout<< "Argument4 : ";cin>>arg4; //display call value argument function cout<< "\n\nDisplay call function sum() and send value argument"; cout<< "\nValue argument before send to function :"; cout<< "\nArgument 1 = "<<arg1<< " Argument 2 ="<<arg2; cout<< "\nResult of sum = "<<sum(arg1,arg2); cout<< "\nValue argument after send to function :(not change)"; cout<< "\nArgument 1 = "<<arg1<< " Argument 2 ="<<arg2; // display call reference argument function cout<< "\n\nDisplay call function multiply() and send reference argument"; cout<< "\nValue argument before send to function :"; cout<< "\nArgument 3 = "<<arg3<< " Argument 4 ="<<arg4; cout<< "\nResult of multiply from function= "<<multiply(arg3,arg4); cout<< "\nValue argument after send to function :(changed)"; cout<< "\n\aArgument 3 = "<<arg3<< " Argument 4 ="<<arg4; getch(); } //end main program

float sum(float para1, float para2) //value argument function { return para1+para2; }float multiply(float& para3, float& para4) //reference argument function

Page 15: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

72

{ para3++; //change para3 and return valut to arg3 para4++; //change para4 and return valut to arg4 return para3*para4; }

♦ !Overloaded functionโอเวอรโหลดฟงกชัน(overloaded function) เปนฟงกชันที่สามารถท ํางานไดหลาย ๆ ลักษณะ

ขึ้นอยูกับอารกิวเมนตที่ผูใชก ําหนดให ดังน้ันช่ือฟงกชันประเภทน้ี ขณะเรียกใชจึงสามารถก ําหนดอารกิวเมนตไดหลายลักษณะ

วิธีการสราง overloaded function มีวิธีการสรางเหมือนกับฟงกชันปกติ แตมีการสรางชื่อoverloaded function ใหมีช่ือเหมือนกัน โดยกํ าหนดพารามิเตอรแตละฟงกชันใหแตกตางกัน ดังตัวอยางในโปรแกรม

• ! โปรแกรม over_fun.cpp แสดงการสราง overloaded function ชื่อฟงกชัน line()

/*Program : over_fun.cpp Process : creat and call overloaded function line()*/#include <iostream.h>#include <conio.h>//declaration overloaded prototype functionvoid line();void line(int x);void line(char ch,int x);void line(char ch);void main() //begin main program{ clrscr(); cout<< "Display 1 line from line() function\n"; line(); //call line() cout<< "Display 2 line from line() function\n"; line(30); //call line() cout<< "Display 3 line from line() function\n"; line('*',60); //call line()cout<< "Display 4 line from line() function\n"; line('+'); //call line() getch();} //end main programvoid line() //overload function1 line()

Page 16: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

73

{ cout<< "================================\n";}

void line(int x) //overload function2 line(){ int y; for (y=1;y<=x;++y) cout<< "_"; cout<<'\n';}

void line(char ch,int x) //overload function3 line(){ int y; for (y=1;y<=x;++y) cout<< ch; cout<<'\n';}

void line(char ch) //overload function4 line(){ int y; for (y=1;y<=80;++y) cout<< ch; cout<<'\n';}

♦ !Default argumentการประกาศ prototype function สามารถก ําหนดคาเร่ิมตน (default) ใหกับฟงกชันได ในขณะ

เรียกใชฟงกชันท ําใหเราสามารถก ําหนดอารกิวเมนตไดหลายลักษณะ ถึงแมเราไมก ําหนด หรือกํ าหนดอารกิวเมนตเพียงบางสวน ฟงกชันก็ยังสามารถทํ างานได เรียกอารกิวเมนตแบบน้ีวา default argument

• ! ตัวอยางโปรแกรม default.cpp แสดงการสรางฟงกชันแบบกํ าหนด default argument ไวสํ าหรับการเรียกใชพังกชัน

/*Program : argument.cpp

Page 17: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

74

Process : creat and call default argument function line()*/#include <iostream.h>#include <conio.h>

//ประกาศ prototype function และกํ าหนด default argumentvoid line(char ch= '_',int x=80);

void main() //begin main program{ clrscr(); cout<< "Display line from line() function\n"; line(); //call line() function line('+'); //call line() function line('#',50); //call line() function line(65); //call line() will display character of ASCII CODE 65 getch();}//end main program

void line(char ch,int x) //function line(){ for (int y=1;y<=x;++y) cout<< ch; cout<<'\n';}

♦ !ลักษณะการใชตัวแปรในฟงกชันการใชหรือการประกาศตัวแปรในโปรแกรมของ C++ สามารถก ําหนดใหตัวแปรมีลักษณะการ

ใชงานได 3 ลักษณะ คือ1. automatic variable เปนการประกาศตัวแปร อยูใน { } ของฟงกชันใดฟงกชันหนึ่ง ตัว

แปรประเภทน้ีจะนํ าไปใชไดเฉพาะในฟงกชันนี้เทานั้น จึงเรียกอีกอยางหน่ึงวา Local variable การท ํางานของตัวแปรประเภทน้ี จะจองพ้ืนท่ีหนวยความจํ าเมื่อฟงกชันถูกเรียกใชเทานั้น และยกเลิกการใชหนวยความจํ าเมื่อฟงกชันท ํางานเสร็จ ปกติ C++ จะไมก ําหนดคาเร่ิมตนใหแกตัวแปร automaticvariable ดังน้ันผูใชควรกํ าหนดคาเร่ิมตนใหกับตัวแปรดวยเพ่ือปองกันความผิดพลาด การก ําหนดตัวแปร automatic variable จะใชคียเวิรด auto หรือไมก็ได เชน

void main()

Page 18: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

75

{ int x=50; //declaration x and number are automatic variable in main() auto float number = 0; //used keywoad auto before type and name fo variable statement;

… }

void repeat() //function repeat(){ int y; //declaration y is automatic variable for(y=1;y<=40;++y) //use y from automatic or local variable cout<<'#'; cout<<endl;

}2. external variable เปนการประกาศตัวแปร อยูนอก { } ของทุกฟงกชัน ตัวแปรประเภทน้ี

จะนํ าไปใชไดในทุกฟงกชัน จึงเรียกอีกอยางหน่ึงวา Global variable การท ํางานของตัวแปรประเภทน้ีจะจองพ้ืนท่ีหนวยความจํ าตลอดเวลาท่ีโปรแกรมทํ างาน และยกเลิกการใชหนวยความจ ําเม่ือโปรแกรมท ํางานเสร็จ ปกติ C++ จะไมก ําหนดคาเร่ิมตนใหอัตโนมัติแตอาจจะไมถูกตอง ดังน้ันผูใชควรกํ าหนดคาเร่ิมตนใหกับตัวแปรดวยเพ่ือปองกันความผิดพลาด การก ําหนดตัวแปร external variable จะก ําหนดหรือประกาศไวกอนฟงกชัน main()

int y=0; //declaration y and x are external variablefloat x=0;

void main() //function main(){ int x=50; //declaration x and number are automatic variable in main() statement; …

}

หมายเหตุ ช่ือตัวแปรประเภท automatic และ external สามารถก ําหนดช่ือซ้ํ ากันได เน่ืองจากใชหนวยความจํ าพื้นที่คนละสวนกัน

3. static variable เปนการประกาศตัวแปร อยูใน { } ของฟงกชันใดฟงกชันหนึ่ง ตัวแปรประเภทนี้จะน ําไปใชไดเฉพาะในฟงกชันนี้เทานั้น การท ํางานของตัวแปรประเภทน้ี จะจองพ้ืนท่ีหนวยความจํ าไวตลอดเวลาท่ีโปรแกรมทํ างาน และยกเลิกการใชหนวยความจ ําโปรแกรมจบการทํ างาน เรียกอีกอยางหน่ึงวา static automatic variable จะใชคียเวิรด static นํ าหนาในการประกาศตัวแปร เชน

float Avg(float value)

Page 19: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

76

{ static float total; //declaration static variable in function Avg() static int amount;

total = total+value; //summation of total statement;

}

• ! ตัวอยางโปรแกรม type_var.cpp แสดงการกํ าหนดและการใชตัวแปรประเภท automaticและ external variable ในโปรแกรม

/*Program : type_var.cpp Process : show using automatic and external variable*/#include <iostream.h>#include <conio.h>

void line(); //declaration prototype functionvoid repeat();int x; //declaration external or global variable

void main() //begin main program{ int x; //declaration automatic or global variable in main() auto int y; //declaration automatic or global variable in main() clrscr(); line(); repeat(); for(y=1;y<=5;++y) //use y is automatic or local variable line(); for(x=1;x<=5;++x) //use x is automatic or local variable repeat();getch();} //end main program

void line() //function line(){ for(x=1;x<=80;x++) //use x from global or external variable cout<<'='; cout<<endl; }

void repeat() //function repeat(){int y;

Page 20: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

77

for(y=1;y<=40;++y) //usy y from automatic or local variable cout<<'#'; cout<<endl;}

♦ !Template Function เท็มเพลตฟงกชัน (Template Function) หมายถึง ฟงกชันที่มีคุณสมบัติในการใชอากิวเมนต

(argument) ไดกับขอมูลหลาย ๆ ประเภท เท็มเพลตฟงกชันก ําหนดใหพารามิเตอรของฟงกชันสามารถรับคาอารกิวเมนตไดหลายประเภท เชน ฟงกชัน sum() กํ าหนดใหเปนประเภท template functionสามารถรับคาจากตัวแปรประเภท int, float และ double ได คํ านวณมาหาผลรวมไดท้ังอารกิวเมนตประเภท int, float และ double ซ่ึงในฟงกชันแบบปกติพารามิเตอรจะรับอารกิวเมนตไดเฉพาะขอมูลประเภทเดียวกันกับที่ก ําหนดไวเทาน้ัน

การก ําหนด template function กํ าหนดไวกอนหนาฟงกชัน main() มีรูปแบบดังน้ีtemplate <class Type>Type Sum(Type first, Type second) //function sum(){ statement; statement; return first+second;}

สวนประกอบใน template function มีดังน้ี1. template เปนคียเวิรดเพ่ือกํ าหนดวา ฟงกชัน sum() เปน template function2. <class Type> class เปนคียเวิรดเพ่ือกํ าหนดชนิดขอมูล มีช่ือวา Type ซึ่งสามารถก ําหนด

ช่ือไดตามความตองการของผูใช ตามกฎการต้ังช่ือของ C++3. Type Sum(Type first, Type second) ชื่อฟงกชัน sum() มีประเภทเปน Type มีพารา

มิเตอร 2 ตัว มีชนิดเปน Type เชนกัน

• ! ตัวอยางโปรแกรม template.cpp แสดงการใชฟงกชันประเภท template function ชื่อ sum()ทํ าหนาท่ีค ํานวณหาผลรวมของเลข 2 จ ํานวน ฟงกชันชื่อ multiply() หาผลคูณเลข 2จ ํานวน ซึ่งเปนตัวเลขประเภท integer, float และ double

/*Program : template.cpp Process : show create and call template function */

Page 21: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

78

#include <iostream.h> #include <conio.h> //declaration templat function sum() template<class TYPE> TYPE sum(TYPE first, TYPE second) { return first+second; } //declare template function multiply() template<class New_type> New_type multiply(New_type number1,New_type number2) { return number1*number2; }void main() //begin main program { int x=50,y=30; float a=300.25, b=100.50; double c=3.21541005,d=10005.02541152; //begin statement clrscr(); cout<< " Sum integer "<< sum(x,y)<<endl; cout<< " Sum float "<< sum(a,b)<<endl; cout<< " Sum double "<< sum(c,d)<<endl; cout<< "\n Multiply integer "<< multiply(x,y)<<endl; cout<< " Multiply float "<< multiply(a,b)<<endl; cout<< " Multiply double "<< multiply(c,d); getch(); } //end main program

Page 22: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

79

♦ !แบบฝกหัดทายบท

1. ใหเขียนโปรแกรมสรางฟงกชันเสนกรอบสี่เหลี่ยม โดยกํ าหนดพารามิเตอรกํ าหนดตํ าแหนง มุมซาย ดานบนและมุมขวาดานลางของกรอบได เชน frame(5,2,50,10) แสดงวากรอบสี่เหลี่ยมจะเริ่มวาด ต้ังแตตํ าแหนง มุมซายบนที่ คอลัมน 5 แถว 2 และมุมขวาลางที่ตํ าแหนง คอลัมน 50 แถวที่ 10

5,2

50,10

2. ใหเขียนโปรแกรมเพ่ือสรางฟงกชันคํ านวณคา factorial(n) เพ่ือคํ านวณหาคาแฟกทอเรียลของจํ านวน n

3. ใหเขียนโปรแกรมเพื่อสรางฟงกชันแบบพารามิเตอรและมีการ return value ในการค ํานวณเลขยกกํ าลังโดยการสง argument เปนเลขจํ านวนฐานและเลขก ําลังไปใหแกฟงกชัน เชน power(2,4)คือการคํ านวณเลขจํ านวน 2 ยกกํ าลัง 4

4. ใหเขียนโปรแกรมเพ่ือสรางฟงกชันประเภทมีพารามิเตอรและมีการ return value ในการค ํานวณหาคาพ้ืนท่ีของรูปเรขาคณิตตอไปน้ี วงกลม, สามเหลี่ยม และ สี่เหลี่ยม

5. จงเขียนโปรแกรมเพ่ือคํ านวณการตัดเกรด โดยการเขียนแยกเปนฟงกชัน มีเง่ือนไขการตัดเกรด ดังน้ีคะแนน 0-49 เกรด F คะแนน 50-59 เกรด Dคะแนน 60-69 เกรด C คะแนน 70-79 เกรด Bคะแนน 80-100 เกรด A

ในโปรแกรมใหสรางฟงกชันตอไปน้ีvoid input() ท ําหนาที่รับคะแนนระหวางภาคและปลายภาคเรียนint summation(int x, int y) ท ําหนาท่ีในการรวมคะแนนchar calculate(int total) ท ําหนาท่ีในการตัดเกรดvoid display() ท ําหนาที่ในการแสดงผลขอมูลของโปรแกรมทั้งหมด

6. ใหเขียนโปรแกรมสรางฟงกชันประเภทมีพารามิเตอรและมกีาร return value เพ่ือคํ านวณหาผลรวมของเลขอนุกรม 2 ฟงกชัน โดยมีชื่อฟงกชัน sum_even() และ sum_odd มีการท ํางาน ดังตัวอยางsum_odd(1,10) คือหาผลรวมของ 1+3+5+7+9sum_odd(9,20) คือหาผลรวมของ 9+11+13+15+17+19

Page 23: บทที่ 05 ฟังก์ชัน (Function)

!

ศิริชัย นามบุรี ฟงกชัน(Functions)

80

sum_even(1,10) คือหาผลรวมของ 2+4+6+8sum_even(9,20) คือหาผลรวมของ 10+12+14+16+18+20

7. Write the function digitsum(n) ,which computes and returns the sum of the decimal digitsof the integer parameter n.

8. Write the function sort4, which has four parameters. If the integer variables a,b,c and d areavailable and have been assigned values, we want to write

sort4(&a, &b,&c,&d);to sort four variables, so that, after that call, we have a<=b<=c<=d