310 likes | 441 Vues
ฟังก์ชั่น. function. ฟังก์ชั่น. เปรียบเสมือนการคำนวณทางคณิตศาสตร์ คำสั่งชุดหนึ่งที่ทำงานเหมือนโปรแกรมหนึ่ง มีชื่อเรียก ส่วนอื่นๆ ของโปรแกรม สามารถเรียกใช้คำสั่งชุดนี้ได้. Main program. Function n. Function 1. Function 2. . . . ฟังก์ชั่นในภาษาซี. ภาษาซีเป็นการเขียนโปรแกรมแบบฟังชั่น
E N D
ฟังก์ชั่น function
ฟังก์ชั่น • เปรียบเสมือนการคำนวณทางคณิตศาสตร์ • คำสั่งชุดหนึ่งที่ทำงานเหมือนโปรแกรมหนึ่ง มีชื่อเรียก • ส่วนอื่นๆ ของโปรแกรม สามารถเรียกใช้คำสั่งชุดนี้ได้
Main program Functionn Function1 Function2 . . . ฟังก์ชั่นในภาษาซี • ภาษาซีเป็นการเขียนโปรแกรมแบบฟังชั่น • แบ่งการทำงานของโปรแกรมเป็นส่วนๆ • ประกอบด้วยฟังก์ชั่นหลัก คือ main()ซึ่งฟังก์ชั่นแรกที่เริ่มทำงาน
ประเภทของฟังก์ชั่น ภาษาซีแบ่งฟังก์ชั่นเป็น 2ประเภท • ฟังก์ชั่นมาตรฐาน (Standard Libraries Function) ฟังก์ชันที่มีอยู่แล้วในภาษาซี โดยกำหนดไว้ในไลบรารี • ฟังก์ชั่นที่ผู้ใช้กำหนด (User-Define Function) ฟังก์ชันที่ผู้ใช้เขียนขึ้นเองใหม่เหมือนกับสร้างฟังก์ชั่นเพิ่มเติมจากที่มีอยู่แล้ว
ฟังก์ชั่นมาตรฐาน เรียกใช้โดยการincludeไฟล์นามสกุล .hที่เก็บฟังก์ชั่น นั้นไว้ เช่น • ถ้าต้องการใช้ฟังก์ชั่นprintfหรือ scanfจะต้องinclude ไฟล์ชื่อstdio.hมาก่อน • ถ้าเราต้องการใช้ฟังก์ชันทางคณิตศาสตร์ เช่น sqrt หรือ powต้องincludeไฟล์ชื่อmath.h คำแนะนำ: ลองเปิดไฟล์ .hในc:\tc\include
Square root 4 = 2.000000 5 power 3 = 125.000000 #include<stdio.h> #include<math.h> void main() { printf(“square root 4 =”); printf(“%f\n”, sqrt(4)); printf(“5 power 3 =”); printf(“%f”, pow(5,3)); } ฟังก์ชั่นมาตรฐาน
ฟังก์ชั่นที่ผู้ใช้กำหนดฟังก์ชั่นที่ผู้ใช้กำหนด • ฟังก์ชันที่เขียนขึ้นเองใหม่เพื่อให้ทำงานตามต้องการ • นิยมเขียนเพื่อทำงานอย่างใดอย่างหนึ่ง • สามารถเรียกใช้ฟังก์ชันนี้หลายๆ ที่ได้ • ประโยชน์ • ทำให้ทั้งโปรแกรมมีโครงสร้างที่ดี กะทัดรัด เข้าใจง่าย • ง่ายต่อการทดสอบและแก้ไขโปรแกรม • นำกลับมาใช้ง่ายและรวดเร็ว
Begin ==== MENU ==== a) Say Hello b) Say Good Bye Select a or b : END #include<stdio.h> void showmenu() { printf(“==== MENU ====\n\n”); printf(“a) Say Hello\n”); printf(“b) Say Good Bye\n”); printf(“Select a or b : \n”); } void main() { printf(“Begin\n”); showmenu(); printf(“END\n”); } ฟังก์ชั่นที่ผู้ใช้กำหนด
รูปแบบของฟังก์ชั่นกำหนดเองรูปแบบของฟังก์ชั่นกำหนดเอง ประกอบด้วย 2ส่วน • ส่วนหัว (Function header) type function_name (type arg1,type arg2,…) • ส่วนตัว (Function body) { variable declaration; statements; [ return ] }
รูปแบบของฟังก์ชั่นกำหนดเองรูปแบบของฟังก์ชั่นกำหนดเอง Parameters = รับค่าเข้า Return= ส่งค่ากลับ type function_name (type arg1,type arg2,…) { [ variable declaration; ] statements; [ return value; ] } ส่งค่ากลับ
ประเภทของฟังก์ชั่นกำหนดเองประเภทของฟังก์ชั่นกำหนดเอง • แบบที่ 1ไม่มีการรับค่าเข้า ไม่มีการส่งค่ากลับ • แบบที่ 2ไม่มีการรับค่าเข้า มีการส่งค่ากลับ • แบบที่ 3มีการรับค่าเข้า ไม่มีการส่งค่ากลับ • แบบที่ 4มีการรับค่าเข้า มีการส่งค่ากลับ
แบบที่ 1: ไม่มีการรับค่าเข้า ไม่มีการส่งค่ากลับ void function_name (void) { statements; }
แบบที่ 2: ไม่มีการรับค่าเข้า มีการส่งค่ากลับ type function_name (void) { statements; return value; }
แบบที่ 3: มีการรับค่าเข้า ไม่มีการส่งค่ากลับ void function_name (type arg1, type arg2,… ) { statements; }
แบบที่ 4: มีการรับค่าเข้า มีการส่งค่ากลับ type function_name (type arg1, type arg2,… ) { statements; return value; }
ฟังก์ชั่นที่กำหนดเอง ต้องมีการ ประกาศฟังก์ชั่นให้รู้จักก่อน จึง สามารถเรียกใช้งานได้ ทำได้โดย การcopy บรรทัดหัวฟังก์ชัน ขึ้นมาเขียนเอาไว้เหนือ main (อย่าลืมใส่เครื่องหมาย ; ด้วย) * ถ้าสร้างฟังก์ชั่นก่อน main ไม่จำเป็นต้องประกาศ การประกาศฟังก์ชั่น(function prototype)
การประกาศฟังก์ชั่น(function prototype) • สำหรับฟังก์ชั่นที่มีการรับค่าเข้า ไม่จำเป็นต้องระบุชื่อargumentก็ได้ เช่น int test_function(int y); หรือ int test_function(int); void test_function(int y); หรือ void test_function(int);
ขอบเขตตัวแปร ตัวแปรในภาษาซีแบ่งเป็น 2ชนิดตามขอบเขตการมองเห็น • Global variable นำไปใช้ได้ในทุกๆ ฟังก์ชันของโปรแกรมนั้น โดยต้องประกาศตัวแปรไว้เหนือฟังก์ชันmain() • Local variable ใช้ได้เฉพาะในฟังก์ชันที่ประกาศตัวแปรนี้เท่านั้น โดยการประกาศจะอยู่ภายในของแต่ละฟังก์ชัน
#include <stdio.h> int function_add (int x, int y) { printf("x = %d , y = %d \n",x,y); return x+y; } main () { int value1 =1, value2 = 2; printf("result : x + y = %d\n“,function_add(value1,value2)); }
void oddeven (void) { if (num % 2 ) printf("Number is odd\n"); else printf("Number is even\n"); } void negpos( void) { if (num < 0) printf("number is negative\n"); else printf("number is positiven\"); } #include <stdio.h> void oddeven (void); void negpos(void); int num; void main (void) { printf("enter number :"); scanf("%d",&num); oddeven(); negpos(); getchar(); }
#include <stdio.h> #define PI 3.14159 float area (float); void main (void) { float radius; printf("Enter radius of sphere:"); scanf("%f",&radius); printf("area of sphere is %.2f",area(radius)); } float area (float rad) { return 4*PI*rad*rad; }
#include <stdio.h> float power3(float); void main (void) { int i; for (i=1; i <=10; i++) printf(“%d\t%.0f\n", i, power3(i)); } float power3(float x) { return x* x*x; } • 1 • 8 • 27 • 64 • 125 • 216 • 343 • 512 • 729 • 1000
#include<stdio.h> main() { int i; int scores[ARRAYMAX]; float percents[ARRAYMAX]; int g_driver, g_mode; int i, left, top, wide, bottom, deep; for (i = 0; i < ARRAYMAX; i++) { printf("\nEnter score between 0 and %d: ", MAX); scanf("%d", &scores[i]); } for (i = 0; i < ARRAYMAX; i++) percents[i] = ((float) scores[i]) / MAX; printf("\n\n\n\tSCORE\tPERCENT"); for (i = 0; i < ARRAYMAX; i++) printf("\n%d. \t%d\t%3.0f", i + 1, scores[i], (percents[i] * 100)); getch(); detectgraph(&g_driver, &g_mode); initgraph(&g_driver, &g_mode, "..\\bgi"); wide = (int)((getmaxx()) / ((ARRAYMAX * 2 ) + 1)); bottom = getmaxy() - 20; deep = (int) (wide / 4); left = wide; for (i = 0; i < ARRAYMAX; i++) { top = (bottom) - ((int)(p[i] * 300)); bar3d(left, top, (left + wide), bottom, deep, 1); left += (wide * 2); } สร้างฟังก์ชั่นทำไม? Input and prepare data Display data in graph Draw graph
แบ่งโค้ดออกเป็น 3ฟังก์ชั่น ฟังก์ชั่น input_data() ฟังก์ชั่น display_data() ฟังก์ชั่น draw_graph() สร้างฟังก์ชั่นทำไม? Main program Input data Display data Draw graph
#include<stdio.h> void input_data(float); void display_data(float); void make_graph(float); main() { Int I, scores[ARRAYMAX]; float percents[ARRAYMAX]; input_data(percents); display_data(percents); make_graph(percents); } void input_data(float p[]) { …. …. } void display_data(float p[]) { …. …. } void make_graph(float p[]) { …. …. } สร้างฟังก์ชั่นทำไม? Main function Input and prepare data Display data in graph Draw graph
#include<stdio.h> void input_data(float); void display_data(float); void make_graph(float); main() { Int I, scores[ARRAYMAX]; float percents[ARRAYMAX]; input_data(percents); display_data(percents); make_graph(percents); } void input_data(float p[]) { …. …. } void display_data(float p[]) { …. …. } void make_graph(float p[]) { …. …. } สร้างฟังก์ชั่นทำไม? Main function Input and prepare data Display data in graph Draw graph
เขียนโปรแกรมในการคำนวณหาพื้นที่เขียนโปรแกรมในการคำนวณหาพื้นที่ สามเหลี่ยม สี่เหลี่ยมและวงกลม โดยให้สร้างเป็นฟังก์ชั่นดังนี้ showmenu(); tri_area(float, float); rec_area(float, float); cir_area(float); เขียนฟังก์ชั่นและเรียกใช้ดังนี้ Minหาค่าต่ำสุด โดยรับค่าเข้า3 ค่าเป็นจำนวนเต็ม และคืนค่าต่ำสุด Maxหาค่าสุดสุด โดยรับค่าเข้า 3ค่าเป็นจำนวนเต็ม และคืนค่าต่ำสุด ตัวอย่างการเรียกใช้ a = min(34,23,42); b = max(3,2,4); โจทย์ 2 โจทย์ 1
เขียนฟังก์ชั่นในการพิมพ์กราฟแนวนอนเขียนฟังก์ชั่นในการพิมพ์กราฟแนวนอน โดยสร้างฟังก์ชั่นดังนี้ ฟังก์ชั่นชื่อ star ไม่มีการส่งค่ากลับ มีการรับค่าเข้าเป็นเลขจำนวนเต็ม ทำการพิมพ์เครื่องหมาย * เท่ากับจำนวนเลขที่รับค่าเข้ามา แล้วขึ้นบรรทัดใหม่ กำหนดตัวแปรชนิด arrayในการ เก็บอุณหภูมิ 5 วัน รับค่าอุณหภูมิเข้าจากผู้ใช้ พิมพ์อุณหภูมิทั้ง 5 วัน และพิมพ์เครื่องหมาย * เท่ากับค่าอุณหภูมิแต่ละวัน (โดยเรียกใช้ฟังก์ชั่น star) Enter temp day 1: 9 Enter temp day 2: 11 Enter temp day 3: 8 Enter temp day 4: 5 Enter temp day 5: 15 Day 1: 9 ********* Day 2: 11 *********** Day 3: 8 ******** Day 4: 5 ***** Day 5: 15 *************** main() { star(9); star(2); } ********* ** โจทย์ 1 โจทย์ 2
References • คู่มือการเขียนโปรแกรมภาษาซี โดย รศ. ธีรวัฒน์ ประกอบผล • เอกสารประกอบการสอนวิชา โครงสร้างข้อมูลและอัลกอริทึม โดย อ.วิญญู นิรนาทล้ำพงศ์ • http://www.kmitl.ac.th/~kpteeraw/c_pro.htm