241 101 Introduction to Computer Programming 4 Function




















































- Slides: 52

241 -101 Introduction to Computer Programming ����� 4 ������ Function คณาจารยภาควชาวศวกรรมคอมพวเต อร introcom@coe. psu. ac. th Department of Computer Engineering, PSU

241 -101 Introduction to Computer Programming ������ • ��������� • ����������������� • ��������� (recursion) Department of Computer Engineering, PSU 2



241 -101 Introduction to Computer Programming 2. ������� โปรแกรมเรมซบซอนมขนาดใหญ มการเปลยนแปลงโครงสรางของโปรแกรม Unstructured Programming Procedural Programming Department of Computer Engineering, PSU 5

241 -101 Introduction to Computer Programming 2. 1 Unstructured Programming ������ : Program ������������ main program ������� statements data ������� : ����������������������� Department of Computer Engineering, PSU • �������� , 6

241 -101 Introduction to Computer Programming ���� 5. 1 Unstructured Programming #include <stdio. h> main() { int first, second, third; printf(“n F(X) = 3 X + 10 if X > 0n”(; printf(“n F(X) = 10 if X = 0n”(; printf(“n Enter 3 valuesn”(; 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); if (second > 0) printf(“F(%d) is %d”, second, 3*second + 10); else printf(“F(%d) is 10”, second); if (third > 0) printf(“F(%d) is %d”, third, 3*third + 10); else printf(“F(%d) is 10”, third); } Department of Computer Engineering, PSU Code program ซำซอน คำนวณ first คำนวณ second คำนวณ third 7



241 -101 Introduction to Computer Programming 2. 2 Procedural Programming (2/2) ���� main ������ procedure ������������ main ������������ procedures ������������ main program data Procedure 1 Procedure 2 Procedure 3 ���� Procedural Programming ������������ Department of Computer Engineering, PSU 10

241 -101 Introduction to Computer Programming ���� 5. 2 Procedural Programming #include <stdio. h> void get_Fx(int x); main() { int first, second, third; printf(“n F(X) = 3 X + 10 if X > 0n”(; printf(“n F(X) = 10 if X = 0n”(; printf(“n Enter 3 valuesn”(; scanf(“%d %d %d”, &first, &second, &third); 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); } Department of Computer Engineering, PSU • รวมการทำงานแบบเดย วกนไวดวยกน • เปลยนแปลงตวแปร xในการเรยกใชงานแต ละครง 11

241 -101 Introduction to Computer Programming . 3������� C (1/2( ���� Procedure ��� - ������ statements ����������� -ขอมล ����������� ผลลพธ function input output ������ เชน ปอนขอมล 16. 0 Department of Computer Engineering, PSU กลองดำ square root computation ผลลพธทไดคอ 4. 0 12

241 -101 Introduction to Computer Programming 3. ������� C (2/2) • ������ C ��������� (main) ������ • ������������ statement ������ • ���� C �������� 2 ��� - ��������� C FUNCTIONS IN C - ����������� C STANDARD LIBRARY Department of Computer Engineering, PSU PROGRAMMER DEFINED FUNCTION 13

241 -101 Introduction to Computer Programming ��������� C (C Standard Library) • ��������� C compiler �������� C library • ������������ - disk I/O (input/output), standard I/O ex. printf(), scanf(), … string manipulation ex. strlen(), strcpy(), … mathematics ex. sqrt(), sin(), cos(), … etc. . • ������������ include header file ������������ printf(), scanf() ����� #include <stdio. h> ����� sqrt(), sin(), cos() ����� #include <math. h> Department of Computer Engineering, PSU 14

241 -101 Introduction to Computer Programming ���� 5. 3 ��������� C ����������� sqrt() �� math. h ������ (prototype) : ������ num double sqrt(double num) 4. 00 2. 00 #include <stdio. h> #include <math. h> int main() { printf(%". 2 fn", sqrt(16. 0)); printf(%". 2 fn", sqrt(16. 0))); } Department of Computer Engineering, PSU 15

241 -101 Introduction to Computer Programming ���� 5. 4 ��������� C ������ 10 ������� 1 ��� 5 ���������� pow() �� math. h 10. 00 ������ double pow(double base, double exp); 100. 00 ������ baseexp 1000. 00 #include <stdio. h> #include <math. h> int main() { double x=10. 0, y =1. 0; do { 10000. 00 100000. 00 ���� 2 => �������� 5 => ��� printf(“%. 2 fn”, pow(x, y)); y++; } while (y < 6); } Department of Computer Engineering, PSU 16





241 -101 Introduction to Computer Programming 4. 1 Function prototype (2/3) • ������ return-type function-name) parameter-list (; - return-type ����������� void, int, double, char, … - function-name �������� - parameter-list ����������� • ��������������������� Department of Computer Engineering, PSU • ����������� “, ” 21

241 -101 Introduction to Computer Programming 4. 1 Function prototype (3/3) • ����������� ขอมล input function ผลลพธ output กลองดำ parameter-list Department of Computer Engineering, PSU function-name ผลลพธ ชนดเดยวกบ return-type 22

241 -101 Introduction to Computer Programming 4. 2 ������� (function definition) • • ����������������� header ��� algorithm • ������ return-type function-name) parameter-list( - header ��������� ���� ; - algorithm ������� { } รายละเอยดการทำงาน ��� return-type ������ void ������� return �������� Department of Computer Engineering, PSU 23

241 -101 Introduction to Computer Programming • • ���������������� return int add) int a, int b) { int result; result = a+b; return result; } Department of Computer Engineering, PSU void checkresult) int result) { if(result < 0) printf(“result is negative”); if(result == 0) printf(“result is zero”); if(result > 0) printf(“result is positive”) } 24

241 -101 Introduction to Computer Programming ���� 5. 7 ��������� #include <stdio. h> int square(int x); int main() { int a=2, b; b = square(a); printf("b = %d n”, b); return 0; } int square(int x) { int y; y = x*x; return y; } Department of Computer Engineering, PSU 25

241 -101 Introduction to Computer Programming ���� 5. 7 ��������� #include <stdio. h> int square(int x); int main() { อากวเมน int a=2, b; b = square(a); ต printf("b = %d n”, b); return 0; } int square(int { int y; y = x*x; return y; } Department of Computer Engineering, PSU พารามเตอ x) ร ตนแบบฟงกช น function prototype เรยกใชงานฟงก ชน call function นยามฟงกชน function definition 26

241 -101 Introduction to Computer Programming 5. ��������� 1. สงใหทำงาน BOSS (calling function) 3. รายงานผล 2. ทำงาน WORKER (called function) BOSS อาจเปนโปรแกรม main หรอเปนฟงกชนอน WORKER คอฟงกชนทถกเรยกใชงาน Department of Computer Engineering, PSU 27


241 -101 Introduction to Computer Programming ���� 5. 8 • ������������ #include <stdio. h> void my_print(); 1. เรยก my_print() void main() { my_print(); } void my_print() { printf(“Hello world”); } Department of Computer Engineering, PSU main 3. เสรจแลวครบ 2. พมพ Hello world my_print 29


241 -101 Introduction to Computer Programming ���� 5. 10 • ������������ #include <stdio. h> ��������� char my_print(int x); 1. เรยก my_print(5) ให x มคา 5 และให chมารบคาทสงก ลบ void main() { char ch; ch = my_print(5); printf(“%c”, ch); } char my_print(int x) { char lch; scanf(“%c”, &lch); while (x > 0) { printf(“%c”, lch); x--; } return lch; } Department of Computer Engineering, PSU main 3. เสรจแลวครบ ผลเกบในตวแปร ch 2. รบคา พมพ แลวส งคากล บ my_print 31

241 -101 Introduction to Computer Programming ���� 5. 11 • ������������ #include <stdio. h> 1. เรยก my_function() int my_function(); void main() { printf(“%d”, my_function()(; } int my_function() { int a; scanf(“%d”, &a); return a*5; } Department of Computer Engineering, PSU main 3. เสรจแลวครบ คาทสงกลบ จะพมพออกทาง หนาจอ 2. รบคา แลวส งคากล บ my_print 32

241 -101 Introduction to Computer Programming 5. 2 ��������� (1/2) • ����������������������� ����� my_print(); ] ��� ex. 5. 8], my_print(‘a’, 5); ] ��� ex. 5. 9] ex. 5. 8 ex. 5. 9 void my_print() { printf(“Hello world”); } Department of Computer Engineering, PSU void my_print(char ch, int x) { while (x > 0) { printf(“%c”, ch); x--; } } 33




241 -101 Introduction to Computer Programming ����������������� script ������ void script(int space, char c, int time) { int i; for(i=0; i< space; i++) printf(“ “); for(i=0; i< time; i++) printf(“%c”, c); } Department of Computer Engineering, PSU 37


241 -101 Introduction to Computer Programming #include <stdio. h> float get_volt(float I, float R); int main() { float i, r, v; printf(“Enter I : ”(; scanf(“%f”, &i); printf(“Enter R : ”(; scanf(“%f”, &r); v = get_volt(i, r); printf(“Volt = %f n”, v); return 0; } float get_volt(float I, float R) { float V = I*R; return V; } Department of Computer Engineering, PSU 39


241 -101 Introduction to Computer Programming #include <stdio. h> int get_fx(int a); int main() { int x, fx; printf(“Enter x : ”(; scanf(“%d”, &x); fx = get_fx(x); printf(“f(%d) = %d n”, x, fx); return 0; } int get_fx(int a) { if(a>=0) return (a*a)+(2*a)+1; else return 0; } Department of Computer Engineering, PSU 41

241 -101 Introduction to Computer Programming 6. ��������� (recursive function) ������������� ����� Factorial ����� Fibonacci factorial(n) = n * factorial(n-1) n! = n * (n-1)! 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 Department of Computer Engineering, PSU #include<stdio. h> int factorial(int x); int main() { int y = factorial(3); printf("3! = %d“, y); return 0; } int factorial(int x) { if(x <= 1) return 1; else return x* factorial(x-1); } 42

241 -101 Introduction to Computer Programming ��������� (recursive function ) #include<stdio. h> int factorial(int x); int main() { int y = factorial(3); printf("3! = %d“, y); return 0; } int factorial(int x) { if(x <= 1) return 1; else return x* factorial(x-1); } Department of Computer Engineering, PSU 43



241 -101 Introduction to Computer Programming int fib(int x) { if(x == 0) return 0; else if(x == 1) return 1; else if(x > 1) return fib(x-1) + fib(x-2); } Department of Computer Engineering, PSU 46


241 -101 Introduction to Computer Programming ���� 5. 14 ��������� #include <stdio. h> void my_func(); int main() { double x = 1. 1; printf("In main, x = %. 2 f n", x(; my_func(); printf("In main, x = %. 2 f n", x(; ผลลพธ return 0; In main, x = 1. 10 } In my_func, x = 2. 50 void my_func() { In main, x = 1. 10 double x; x = 2. 5; printf("In my_func, x = %. 2 f n", x(; } Department of Computer Engineering, PSU 48

241 -101 Introduction to Computer Programming ���� 5. 15 ��������� #include <stdio. h> double x; void my_func(); int main() { x = 1. 1; printf("In main, x = %. 2 f n", x(; my_func(); printf("In main, x = %. 2 f n", x(; return 0; } void my_func() { x = 2. 5; printf("In my_func, x =%. 2 f n", x(; } Department of Computer Engineering, PSU ผลลพธ In main, x = 1. 10 In my_func, x = 2. 50 In main, x = 2. 50 49

สรปขอบเขตของตวแ ปร #define MAX 950 241 -101 Introduction to Computer Programming ตวแปร รจกใ น รจก ใน MAX one fun_two main anarg(int) second onelocal LIMIT one (parameter) anarg(char) localvar(fun_two) } localvar(main) int main)void) { int localvar; … } ����� ���� ��� ���� one fun_two main void one)int anarg, double second) { int onelocal; … } #define LIMIT 200 int fun_two)int one, char anarg) { int localvar; … Department of Computer Engineering, PSU one (function) fun_two 50

