งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

ฟังก์ชัน (Function).

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "ฟังก์ชัน (Function)."— ใบสำเนางานนำเสนอ:

1 ฟังก์ชัน (Function)

2 จุดประสงค์ เพื่อเข้าใจวิธีการแบ่งโปรแกรมออกเป็นส่วนย่อย โดยใช้หลักการของฟังก์ชัน เข้าใจกระบวนการในการส่งข้อมูลระหว่างฟังก์ชัน เข้าใจวิธีการในการเขียน ใช้ฟังก์ชัน

3 บทนำ โปรแกรมคอมพิวเตอร์ส่วนใหญ่ที่ใช้งานจริง มักจะเป็นโปรแกรมขนาดใหญ่ เกินกว่าที่จะเขียนรวมๆ กันในฟังก์ชันหลัก (main) เทคนิคที่เรียกว่า Divide and Conquer แบ่งปัญหาออกเป็นส่วนเล็กๆ หรือโมดูล (Module) เมื่อสร้างและทดสอบโปรแกรมย่อยๆนั้นแล้ว ก็ประกอบขึ้นมาเป็นโปรแกรมใหญ่ที่สมบูรณ์ในขั้นตอนสุดท้าย

4 Program Module ในภาษา C
FUNCTIONS IN C C STANDARD LIBRARY PROGRAMMER DEFINED FUNCTION

5 Function Algorithm Complex Function 1. สั่งให้ทำงาน 2. ทำงาน
BOSS (calling func) WORKER (called func) 3. รายงานผล Complex Function main worker1 worker3 worker2 worker4 worker5

6 การนิยามฟังก์ชัน #include <stdio.h> main() { int x;
/* 1st sample program: calling standard library function */ #include <stdio.h> main() { int x; for (x = 1; x <= 10; x++) printf(“%d “, x * x); printf(“\n”); return 0; }

7 /* 2nd sample program: A programmer-defined square function */
#include <stdio.h> int square(int);/* function prototype*/ main() { int x; for (x = 1; x <= 10; x++) printf(“%d “, square(x)); printf(“\n”); return 0; } /* Function definition */ int square(int y) return y*y;

8 การนิยามฟังก์ชัน return-value-type function-name (parameter-list) {
declarations statements }

9 return-type-value คือ ชนิดของข้อมูลที่ต้องการจะส่งค่ากลับ เช่น int หรือ float หากไม่มีการส่งค่ากลับให้ใช้ void function-name คือ ชื่อของฟังก์ชันที่จะถูกสร้างขึ้น parameter-list คือรายการตัวแปรพารามิเตอร์ ทั้งหมดที่ต้องการส่งผ่านไปยังฟังก์ชันเมื่อมีการเรียกใช้ ตามลำดับ declarations คือ ส่วนของการประกาศตัวแปรภายในฟังก์ชัน statements คือ ส่วนของคำสั่งที่กระทำในฟังก์ชันนั้น

10 ตัวอย่าง ตัวอย่างเช่น หากต้องการเขียนฟังก์ชันชื่อ foo ไม่มีการส่งผ่านค่ากลับ รับพารามิเตอร์ เป็น จำนวนเต็ม 2 ตัว โดยฟังก์ชันจะคำนวณหาผลบวกของจำนวนทั้งสอง แล้วแสดงผลออกมาทางจอภาพ void foo(int x, int y) { int ans; ans = x + y; printf(“%d”, ans); }

11 หรือถ้าต้องการเขียนฟังก์ชันชื่อ subtract ที่ส่งค่ากลับเป็นจำนวนเต็ม ซึ่งเป็นผลลบของพารามิเตอร์ ที่เป็นจำนวนเต็มตัวแรกลบด้วยจำนวนเต็มตัวที่สอง จะนิยามฟังก์ชันได้ว่า int subtract (int x, int y) { int ans; ans = x - y; return ans; }

12 Function แบบต่าง ๆ รูปแบบตัวอย่างที่ 1: ฟังก์ชันที่ไม่รับผ่านค่า และไม่ส่งผ่านค่ากลับ void main() { my_print(); } void my_print() printf(“Hello world”);

13 รูปแบบตัวอย่างที่ 2: ฟังก์ชันที่มีการรับผ่านค่า แต่ไม่ส่งผ่านค่ากลับ
/* 2.1 */ void main() { my_print(2); } void my_print(int x) printf(“%d”, x); main my_print 1. ทำงานด้วย 2 ! 2. พิมพ์ 3. เสร็จแล้วครับ

14 void my_print(char ch, int x) while (x > 0) printf(“%c”, ch); x--;
/* 2.2 */ void main() { my_print(‘a’, 5); } void my_print(char ch, int x) while (x > 0) printf(“%c”, ch); x--; my_print 1. ทำงานด้วย ‘a’ และ 5 ! 2. พิมพ์ 3. เสร็จแล้วครับ main

15 รูปแบบตัวอย่างที่ 3: ฟังก์ชันที่มีการรับผ่านค่า และส่งผ่านค่ากลับ
void main() { char ch; ch = my_print(5); printf(“%c\n”, ch); }

16 printf(“Enter your character: ”); scanf(“%c”, &lch); while (x > 0)
char my_print(int x) { char lch; printf(“Enter your character: ”); scanf(“%c”, &lch); while (x > 0) printf(“%c”, lch); x--; } printf(“\n”); return lch; main my_print 1. ทำงานด้วย 5 ! 2. รับค่าแล้วพิมพ์ 3. เสร็จแล้วครับ ผลคือ ch

17 รูปแบบตัวอย่างที่ 4: ฟังก์ชันที่ไม่รับผ่านค่า แต่มีการส่งผ่านค่ากลับ
void main() { printf(“%d”,my_func()); } int my_func() int a; scanf(“%d”,&a); return a*5;

18 ต้นแบบของฟังก์ชัน (Function Prototype)
ต้นแบบของฟังก์ชัน จะเป็นตัวระบุให้ตัวแปลภาษารู้ถึง ชนิดของข้อมูลที่จะส่งผ่านค่ากลับ จำนวนของพารามิเตอร์ ที่ฟังก์ชันคาดหวังว่าจะได้รับ ชนิดของพารามิเตอร์แต่ละตัว และลำดับของพารามิเตอร์เหล่านั้น

19 PROTOTYPE CALLING #include <stdio.h> int maximum(int, int, int);
/* 3rd Sample program */ /* Finding the maximum of three integers */ #include <stdio.h> int maximum(int, int, int); main() { int a, b, c; printf(“Enter three integers: “); scanf(“%d%d%d”, &a, &b, &c); printf(“Maximum is: %d\n”, maximum(a, b, c); return 0; } PROTOTYPE CALLING

20 DEFINITION int maximum(int x, int y, int z) { int max = x;
/* Function maximum definition */ int maximum(int x, int y, int z) { int max = x; if(y > max) max = y; if(z > max) max = z; return max; } DEFINITION

21 PROTOTYPE DEFINITION CALLING #include<stdio.h> int square(int);
/* 4th Sample Program */ #include<stdio.h> int square(int); void main( ) { int x=3; printf(“Square of %d = %d\n”, x, square(x)); } int square(int y) { return y * y; PROTOTYPE CALLING DEFINITION

22 PROTOTYPE DEFINITION CALLING #include<stdio.h>
/* 5th Sample Program */ #include<stdio.h> int maximum(int, int, int); void main() { int a, b, c; printf(“Enter three integer: “); scanf(“%d%d%d”, &a, &b, &c); printf(“Maximum is: %d\n”, maximum(a, b, c)); } int maximum(int x, int y, int z) int max = x; if(y > max) max = y; if(z > max) max = z; return max; PROTOTYPE CALLING DEFINITION

23 DEFINITION CALLING #include<stdio.h>
/* 6th Sample Program */ #include<stdio.h> int maximum(int x, int y, int z) { int max = x; if(y > max) max = y; if(z > max) max = z; return max; } void main( ) int a, b, c; printf(“Enter three integer: “); scanf(“%d%d%d”, &a, &b, &c); printf(“Maximum is: %d\n”, maximum(a, b, c)); DEFINITION CALLING

24 ตัวแปรภายใน และตัวแปรภายนอก
ตัวแปรภายใน (Local Variable) คือ ตัวแปรที่ถูกสร้างขึ้นภายในฟังก์ชัน สามารถเรียกใช้งานได้เฉพาะภายในฟังก์ชันที่สร้างขึ้น และจะถูกทำลายลงเมื่อเสร็จสิ้นการทำงานของฟังก์ชันนั้นๆ ตัวแปรภายนอก (Global Variable) คือ ตัวแปรที่ถูกสร้างขึ้นภายนอกฟังก์ชัน สามารถเรียกใช้งานได้ในทุกฟังก์ชัน หรือทั้งโปรแกรม

25 Global Variable Local Variable Local Variable
/* 7th Sample Program: Local vs Global Variable */ #include<stdio.h> int ans = 0; int inc_one(int); /* function prototype */ void main() { int a = 3; ans = inc_one(a); printf(“Answer is %d\n”, ans); } /* function definition: return x+1 */ int inc_one(int x) int ans; ans = x + 1; return ans; Global Variable Local Variable Local Variable

26 Global Variable Global Variable Global Variable
/* 9th Sample Program: Global Variables */ #include<stdio.h> int x; void my_func(); void main() { x=3; printf(“Main: Before call function x=%d\n”, x); my_func(); //call my_func printf(“Main: After call function x=%d\n”, x); } void my_func() x=2; printf(“My_func: x=%d\n”, x); Global Variable Global Variable Global Variable

27 Global Variable Global Variable Local Variable
/* 10th Sample Program: Local vs. Global Variables */ #include<stdio.h> int x; void my_func(); //prototype of my_func void main() { x=3; printf(“Main: Before call function x=%d\n”, x); my_func(); //call my_func printf(“Main: After call function x=%d\n”, x); } void my_func() x=2; printf(“My_func: x=%d\n”, x); Global Variable Global Variable Local Variable

28 เป็น argument ของฟังก์ชันอื่น
ตัวอย่างการเรียกใช้ฟังก์ชันที่มีการส่งผ่านค่ากลับ void main() { int x, y; printf(“%d”,my_func()); x = my_func(); y = 45 * x + my_func(); } int my_func() int a; scanf(“%d”,&a); return a*5; เป็น argument ของฟังก์ชันอื่น มีตัวแปรมารับค่า Lป็นส่วนหนึ่งของนิพจน์

29 ตัวอย่างโปรแกรมที่ไม่ใช้ฟังก์ชัน
#include <stdio.h> main() { int first, second, third; printf(“\n F(X) = 3X + 10 if X > 0\n”); printf(“\n F(X) = 10 if X = 0\n”); printf(“\n Enter 3 values\n”); scanf(“%d %d %d”, &first, &second, &third); if (first > 0) printf(“F(%d) is %d”,first,3*first+10); else printf(“F(%d) is 10”, first);

30 if (second > 0) printf(“F(%d) is %d”, second, 3*second + 10); else printf(“F(%d) is 10”, second); if (thrid > 0) printf(“F(%d) is %d”, third, 3*third + 10); printf(“F(%d) is 10”, third); }

31 ตัวอย่างโปรแกรมที่ใช้ฟังก์ชัน
#include <stdio.h> void get_Fx(int x); main() { int first, second, third; printf(“\n F(X) = 3X + 10 if X > 0\n”); printf(“\n F(X) = 10 if X = 0\n”); printf(“\n Enter 3 values\n”); scanf(“%d %d %d”, &first, &second, &third);

32 get_Fx(first); get_Fx(second); get_Fx(third); } void get_Fx(int x) { if (x > 0) printf(“F(%d) is %d”, x, (3*x) + 10); else printf(“F(%d) is 10”, x);

33 Math routines Function Include File abs stdlib.h acos math.h
asin math.h atan math.h atof stdlib.h atoi stdlib.h cos math.h cosh math.h

34 Function Include File exp math.h itoa stdlib.h log math.h log10 math.h sin math.h sinh math.h sqrt math.h tan math.h tanh math.h


ดาวน์โหลด ppt ฟังก์ชัน (Function).

งานนำเสนอที่คล้ายกัน


Ads by Google