240 101 Introduction to computer programming Program Module

  • Slides: 36
Download presentation
240 -101 Introduction to computer programming ?

240 -101 Introduction to computer programming ?

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

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

Function Algorithm BOSS )calling func( . 1 สงใหทำง าน. 3รายงานผล . 2ทำงาน WORKER )called

Function Algorithm BOSS )calling func( . 1 สงใหทำง าน. 3รายงานผล . 2ทำงาน WORKER )called func( Complex Function main worker 1 worker 4 worker 2 worker 5 worker 3

การนยามฟงกชน /* 1 st sample program: calling standard library function */ #include <stdio. h>

การนยามฟงกชน /* 1 st 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; }

/* 2 nd sample program: A programmer-defined square function */ #include <stdio. h> int

/* 2 nd 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; }

main( ) square( ) COPY ARGUMENTS x print f y y * y COPY

main( ) square( ) COPY ARGUMENTS x print f y y * y COPY RETURNVALUE

การนยามฟงกชน return-value-type function-name (parameterlist) { declarations statements }

การนยามฟงกชน return-value-type function-name (parameterlist) { declarations statements }

char my_print(int x) { char lch; printf(“Enter your character: ”); scanf(“%c”, &lch); . 1ทำงานดวย

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

/* 3 rd Sample program */ /* Finding the maximum of three integers */

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

/* Function maximum definition */ int maximum(int x, int y, int z) { int

/* 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

/* 4 th Sample Program */ #include<stdio. h> PROTOTYPE int square(int); void main( )

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

/* 5 th Sample Program */ #include<stdio. h> int maximum(int, int); void main() {

/* 5 th Sample Program */ #include<stdio. h> int maximum(int, int); void main() { int a, b, c; printf(“Enter three integer: “); CALLING scanf(“%d%d%d”, &a, &b, &c); printf(“Maximum is: %dn”, 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 DEFINITION

/* 6 th Sample Program */ #include<stdio. h> int maximum(int x, int y, int

/* 6 th 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; } DEFINITION void main( ) { int a, b, c; CALLING printf(“Enter three integer: “); scanf(“%d%d%d”, &a, &b, &c); printf(“Maximum is: %dn”, maximum(a, b, c)); }

/* 7 th Sample Program: Local vs Global Variable */ #include<stdio. h> int ans

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

/* 9 th Sample Program: Global Variables */ #include<stdio. h> int x; void my_func();

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

/* 10 th Sample Program: Local vs. Global Variables */ #include<stdio. h> int x;

/* 10 th 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=%dn”, x); my_func(); //call my_func printf(“Main: After call function x=%dn”, x); } void my_func() { int x; x=2; printf(“My_func: x=%dn”, x); } Global Variable Local Variable

ตว อยางโปรแกรมทไมใชฟงกชน #include <stdio. h> main() { int first, second, third; printf(“n F(X) =

ตว อยางโปรแกรมทไมใชฟงกชน #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

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); else printf(“F(%d) is 10”, third); }

ตวอยางโปรแกรมทใชฟงกชน #include <stdio. h> void get_Fx(int x); main() { int first, second, third; printf(“n

ตวอยางโปรแกรมทใชฟงกชน #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

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); }

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

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

Function exp itoa log 10 sinh sqrt tanh Include File math. h stdlib. h

Function exp itoa log 10 sinh sqrt tanh Include File math. h stdlib. h math. h