# Function function function Jirasak Sittigorn http www kmitl

• Slides: 59
Download presentation

ฟงกชน (Function) แบงออกเปน • ฟงกชนมาตรฐานไลบราร function) • ฟงกชนทสรางขนเอง function) Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 2 ประเภท 01001012 Principle of Computer Programming (Standard Library (User Defined 8 King Mongkut's Institute of Technology

ตวอยางการใชงานฟงกชน sine 01001012 Principle of Computer Programming #include<stdio. h> #include<math. h> #define PI 22. 0/7 int main() { float deg=0; do{ if ((int)deg%30==0 || (int)deg%45==0) printf ( "Sine %3 d is %5. 2 fn", (int)deg, sin(deg*PI/180)); deg += 15; } while (deg < 360); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 10 King Mongkut's Institute of Technology

ฟงกชนการคำนวณทางคณตศาสตร 01001012 Principle of Computer Programming #include<math. h> sin(var); cos(var); tan(var); sqrt(var); pow(var 1, var 2); log(var); log 10(var); exp(var); fabs(var); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 13 King Mongkut's Institute of Technology

ตวอยางการใชงานฟงกชนการคำนวณทางคณต ศาสตร 01001012 Principle of Computer Programming #include<stdio. h> #include<math. h> #define PI 22/7. 0 int main() { float deg, rad; printf ("Enter Degree : "); scanf ("%f", &deg); rad = deg * PI / 180; printf ("sin(%. 2 f) = %. 2 fn", deg, sin(rad)); printf ("cos(%. 2 f) = %. 2 fn", deg, cos(rad)); printf ("tan(%. 2 f) = %. 2 fn", deg, tan(rad)); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 14 King Mongkut's Institute of Technology

ฟงกชนสำหรบขอความ และตวอกษร 01001012 Principle of Computer Programming #include<string. h> strcpy(str 1, str 2); strcat(dest 1, src 2); strcmp(str 1, src 2); strcmpi(str 1, str 2); strlen(str); #include<ctype. h> tolower(ch); toupper(ch); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 15 King Mongkut's Institute of Technology

ตวอยางการใชงานฟงกชนสำหรบขอความ (1) (A) 01001012 Principle of Computer Programming str 1 => C Language #include<stdio. h> str 2 => is easy #include<string. h> str 3 => int main() { str 4 => C Language is easy int test; char str 1[20] = "C Language "; char str 2[20] = "is easy"; char str 3[20]; char str 4[20] = "C Language is easy"; printf ("Old stringn", str 1); printf ("str 1 : %sn", str 1); printf ("str 2 : %sn", str 2); printf ("str 3 : %sn", str 3); printf ("str 4 : %sn", str 4); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 16 King Mongkut's Institute of Technology

ตวอยางการใชงานฟงกชนสำหรบขอความ (2) (A) 01001012 Principle of Computer Programming strcpy(str 3, str 1); strcat(str 1, str 2); str 1 => C Language is easy str 2 => is easy str 3 => C Language str 4 => C Language is easy printf printf ("nn. New stringn", str 1); ("str 1 : %sn", str 1); ("str 2 : %sn", str 2); ("str 3 : %sn", str 3); ("str 4 : %sn", str 4); printf ("nnlength of str 1 = %d", strlen(str 1)); ("nlength of str 2 = %d", strlen(str 2)); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 17 King Mongkut's Institute of Technology

ตวอยางการใชงานฟงกชนสำหรบขอความ (3) test = printf return (A) 01001012 Principle of Computer Programming strcmp(str 1, str 2); ("nnstrcmp (str 1 vs str 2) = %d", test); strcmp(str 1, str 4); ("nnstrcmp (str 1 vs str 4) = %d", test); strcmpi(str 1, str 2); ("nnstrcmpi (str 1 vs str 2) = %d", test); strcmpi(str 1, str 4); ("nnstrcmpi (str 1 vs str 4) = %d", test); 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 18 King Mongkut's Institute of Technology

ตวอยางการใชงานฟงกชนสำหรบขอความ (B) 01001012 Principle of Computer Programming #include<stdio. h> #include<string. h> int main() { char s 1[]="Kmitl"; char s 2[]="KMITL"; printf ("strcmp (s 1, s 2) = %dn", strcmp(s 1, s 2)); printf ("strcmpi(s 1, s 2) = %dn", strcmpi(s 1, s 2)); return 0; } strcmp (s 1, s 2) = 32 strcmpi(s 1, s 2) = 0 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 19 King Mongkut's Institute of Technology

ตวอยางการใชงานฟงกชนสำหรบอกขระ 01001012 Principle of Computer Programming #include<stdio. h> #include<ctype. h> int main() { int i=0; char s 1[]="Kmitl"; char s 2[]="KMITL"; printf ("Old Stringn"); printf ("s 1=%s s 2=%s", s 1, s 2); for ( ; s 1[i]!=''; i++) { s 1[i]=toupper(s 1[i]); s 2[i]=tolower(s 2[i]); } printf ("n. New Stringn"); printf ("s 1=%s s 2=%s", s 1, s 2); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 20 King Mongkut's Institute of Technology

ฟงกชนทสรางขนเอง (2) #include<file. h> (User-defined Function) 01001012 Principle of Computer Programming พรโปรเซสเซอรไดเรคทฟ function_name(type); ฟงกชนโพรโทรไทพ ตวแปรชนดโกบอล variable type int main() { type variable; ตวแปรชนดโลคอล statement-1; คำสง. . . statement-n; return 0; } type function_name(type variable) { statement-1; . . . statement-n; return(var); } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ สวนหวโปรแกรม ฟงกชนหลก สวนตวโปรแกรม ฟงกชนยอย 22 King Mongkut's Institute of Technology

ฟงกชนทสรางขนเอง (3) (User-defined Function) 01001012 Principle of Computer Programming • รปแบบ & ลกษณะการทำงานของฟงกชนทสรางขนเอง type function-name (type); int main() {. . . y=function-name(x); . . . return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ type function-name (type) {. . . return ? ; } 23 King Mongkut's Institute of Technology

การสรางฟงกชนกอน การสรางฟงกชนหลง #include<file. h> type func_name(type); type variable int main() { type variable; statement-1; . . . statement-n; var = func_name(value); return 0; } type func_name(type variable) { statement-1; . . . statement-n; return(var); } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ VS ฟงกชนหลก 01001012 Principle of Computer Programming #include<file. h> type variable type func_name(type variable) { statement-1; . . . statement-n; return(var); } int main() { type variable; statement-1; . . . statement-n; var = func_name(value); return 0; } 26 King Mongkut's Institute of Technology

ตวอยาง | ผงงาน & ผลลพธ ***** * kmitl * ***** 01001012 Principle of Computer Programming START show_star(n) num=9 i=1 show_star(num) i<=n * Kmitl * F T * i++ show_star(num) return END Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 27 King Mongkut's Institute of Technology

ตวอยาง | โปรแกรม #include<stdio. h> void show_star (int n) { int i; for (i=1; i<=n; i++) putchar('*'); } int main() { int num=9; show_star(num); printf ("n* kmitl *n"); show_star(num); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 01001012 Principle of Computer Programming #include<stdio. h> void show_star (int); int main() { int num=9; show_star(num); printf ("n* kmitl *n"); show_star(num); return 0; } void show_star (int n) { int i; for (i=1; i<=n; i++) putchar('*'); } 28 King Mongkut's Institute of Technology

ฟงกชนทไมมการรบสงคา 01001012 Principle of Computer Programming • เปนฟงกชนท ไมม การรบคา เขามาในฟงก ชน และไมม การสงคากลบ ออกไปจากฟงกชน #include<stdio. h> void function_name() void function_name(void); int main() {. . . function_name(); . . . return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ { statement-1; statement-2; . . . statement-n; } 30 King Mongkut's Institute of Technology

message START 01001012 Principle of Computer Programming i = 0 message False i<30 True "*" i ++ message "C Language" i = 0 message False i<30 "*" i ++ END Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ True return 31 King Mongkut's Institute of Technology

ตวอยางฟงกชนทไมมการรบสงคา (1) 01001012 Principle of Computer Programming #include<stdio. h> void message(void); /* Function Prototype */ int main() { printf ("First ->Call messagen"); message(); printf ("Second ->Call messagen"); message(); printf ("Third ->Call messagen"); message(); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 32 King Mongkut's Institute of Technology

ตวอยางฟงกชนทไมมการรบสงคา (2) 01001012 Principle of Computer Programming void message() { int i=0; while (i<30) { printf ("*"); i++; } printf ("nt. C Languagen"); i=0; while (i<30) { printf ("*"); i++; } printf ("nn"); } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 33 King Mongkut's Institute of Technology

ฟงกชนทมการรบคา แตไมสงคากลบ 01001012 Principle of Computer Programming • เปนฟงกชนท ม การรบคา เขามาในฟงกช น แต ไมม การสงคากลบ ออกไปจากฟงกชน #include<stdio. h> void func_name(type); int main() {. . . func_name(var. X); . . . return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ void func_name(type var. Y) { statement-1; statement-2; . . . statement-n; } 34 King Mongkut's Institute of Technology

(2) START 01001012 Principle of Computer Programming add_number (x, y) num 1, num 2 ans num 1 num 2 add_number (num 1, num 2) ans = x + y ans return END Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 36 King Mongkut's Institute of Technology

โปรแกรมบวกเลข | function (3) 01001012 Principle of Computer Programming #include<stdio. h> void addnumber(int, int); /*Function Prototype*/ int main() { int num 1, num 2; printf") nn. Main Functionnn"); printf") Enter number 1 : "); scanf %") d", &num 1); printf ("Enter number 2 : "); scanf %") d", &num 2); addnumber(num 1, num 2); return 0; } void addnumber(int x, int y) { int ans; printf") nn. Addnumber Functionnn"); ans = x+y; printf") number 1 + number 2 = %d +% d = %d", x, y, ans); } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 37 King Mongkut's Institute of Technology

ฟงกชนทมการรบคา และมการสงคากลบ • เปนฟงกชนท 01001012 Principle of Computer Programming ม การรบคา เขามาในฟงกช น และม การสงคากลบ ออกไปจากฟงกชน #include<stdio. h> type func_name(type); int main() {. . . var = func_name(var. X); . . . return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ type func_name(type var. Y) { statement-1; statement-2; . . . statement-n; return(var. Z); } 38 King Mongkut's Institute of Technology

(2) factorial(x) 01001012 Principle of Computer Programming START i=1, ans=1 num False factorial(num) i<30 True ans=ans*i++ END return(ans) Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 40 King Mongkut's Institute of Technology

โปรแกรมหาแฟกทอเรยล (factorial) ของคาทรบมา function (3) | 01001012 Principle of Computer Programming #include<stdio. h> long double factorial )int); */Function Prototype*/ int main() { int num; printf") Enter number : "); scanf %") d", &num); printf ("%. 0 f", (float)factorial)num)); return 0; } long double factorial)int x) { int i=1; long double ans=1; for ( ; i<=x; ) ans = ans*i++; return ans; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 41 King Mongkut's Institute of Technology

ฟงกชนทไมมการรบคา แตมการสงคากลบ • เปนฟงกชนท ไมม การรบคาเขามาในฟงก 01001012 Principle of Computer Programming ชน แต ม การสงคากลบออกไปจากฟงกชน #include<stdio. h> void func_name(type); int main() {. . . var = func_name(); . . . return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ type func_name() { statement-1; statement-2; . . . statement-n; return(var. Z); } 42 King Mongkut's Institute of Technology

ตวอยางการใชฟงกชนทไมมการรบคา แตมการสงคากลบ 01001012 Principle of Computer Programming #include<stdio. h> int sub_func(void); /*Function Prototype*/ int main() { int value; printf ("Call subfunction to return valuen"); value = sub_func(); printf ("Value from subfunction : %d", value); return 0; } int sub_func(void) { int re_value=5, i; for (i=0; i<10; i++) re_value = (re_value+i)+i; return (re_value); } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 43 King Mongkut's Institute of Technology

Example 1 #include<stdio. h> #include<math. h> 01001012 Principle of Computer Programming #define PI 3. 14 float deg_rad(float); /*Function Prototype*/ int main() { float deg, rad; printf ("Enter Degree : "); scanf ("%f", &deg); rad = deg_rad(deg); //printf ("%f->%fn", deg, rad); printf ("sin(%. 2 f) = %. 3 fn", deg, sin(rad)); printf ("cos(%. 2 f) = %. 3 fn", deg, cos(rad)); printf ("tan(%. 2 f) = %. 3 fn", deg, tan(rad)); return 0; } float deg_rad(float num) { float ans; ans = num * PI / 180; return(ans); } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 45 King Mongkut's Institute of Technology

Example 2 #include<stdio. h> int prime(int); /*Function Prototype*/ 01001012 Principle of Computer Programming int main() { int a, b, i, num; printf ("Enter Number 1 : "); scanf ("%d", &a); printf ("Enter Number 2 : "); scanf ("%d", &b); int prime(int num) for (i=a; i<=b; i++) { { int i, c=0; num=prime(i); for (i=1; i<=num; i++) if (num!=0) { printf ("%d ", num); if (num%i == 0) } c++; return 0; } } if (c==2) return(num); } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ else return(0); 46 King Mongkut's Institute of Technology

#include<stdio. h> int num 1; void test(void); int main() { num 1 = 19; printf ("line 1 test(); printf ("line 2 return 0; } void test() { num 1 = 26; printf ("line 1 } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 01001012 Principle of Computer Programming num 1 /*Function Prototype*/ (main) : num 1 = %dn", num 1); (test) : num 1 = %dn", num 1); 48 King Mongkut's Institute of Technology

#include<stdio. h> void test(void); /*Function int main() { int num 1; num 1 = 19; printf ("line 1 (main) : test(); printf ("line 2 (main) : return 0; } void test() { int num 1; num 1 = 26; printf ("line 1 (test) : } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ Prototype*/ 01001012 Principle of Computer Programming num 1 = %dn", num 1); 49 King Mongkut's Institute of Technology

Pass by Value Function & Pass by Reference Function (1) 01001012 Principle of Computer Programming #include<stdio. h> void func_value(int var) { var = var + 1; printf("In functionn"); printf("Var = %dn", var); } int main() { int x=19; printf("Test by Valuen"); printf("Before functionn"); printf("x = %dn", x); func_value(x); printf("After functionn"); printf("x = %dn", x); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ #include<stdio. h> void func_ref(int *pt) { *pt = *pt + 1; printf("In functionn"); printf("*pt = %dn", *pt); } int main() { int x=19; printf("Test by Refn"); printf("Before functionn"); printf("x = %dn", x); func_ref(&x); printf("After functionn"); printf("x = %dn", x); return 0; } 53 King Mongkut's Institute of Technology

Pass by Value Function & Pass by Reference Function (2) 01001012 Principle of Computer Programming Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 54 King Mongkut's Institute of Technology

Q&A (1) #include <stdio. h> int a; void func(int x) { x=10; printf ("x = %dn", x); } int main() { int b; a = 3; b = 5; func(b); printf ("a = %dn", a); printf ("b = %dn", b); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 01001012 Principle of Computer Programming 56 King Mongkut's Institute of Technology

Q&A #include <stdio. h> int a; void func(int *x) { *x=10; printf ("x = %dn", *x); } int main() { int b; a = 3; b = 5; func(&b); printf ("a = %dn", a); printf ("b = %dn", b); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ (2) 01001012 Principle of Computer Programming 57 King Mongkut's Institute of Technology

Q&A #include <stdio. h> int a; void func(int x) { a=10; printf ("x = %dn", x); } int main() { int b; a = 3; b = 5; func(b); printf ("a = %dn", a); printf ("b = %dn", b); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ (3) 01001012 Principle of Computer Programming 58 King Mongkut's Institute of Technology

Q&A #include <stdio. h> int a; void func(int x) { int a=10; printf ("x = %dn", x); } int main() { int b; a = 3; b = 5; func(b); printf ("a = %dn", a); printf ("b = %dn", b); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ (4) 01001012 Principle of Computer Programming 59 King Mongkut's Institute of Technology