6/1
(Function)
type                                             int ,float , char , double , void                                        ...
F N T N INC                         U C IO S             CS A D R               TNAD                  PO RM E             ...
1.1.1type function_name(type1 arg1, type2 arg2,.., typeN argN)  {  local variable declaration;  statement(s);  return( var...
type                        type             int, float, chardouble                                  int                  ...
#include<stdio.h>#include<conio.h>void one(void); /* function Prototypevoid two(void); /* function Prototypevoid main(void...
3         4       void one(void);         void two(void);                        one( )        two( )                     ...
            
1.2          2#include <stdio.h>   #include <stdio.h>Main ()              Main (){                    {Function-name2 ()...
    #include
1.3                                  ()      void function_name(void )
#include<stdio.h>#include<conio.h>void asterisk_line(void);void main(void){clrscr( );asterisk_line( );printf("****** C and...
3      void asterisk_line (void);    asterisk_line( )    7    9                             asterisk_line()               ...
1.4      void function_name (type_parameter parameter_name [,..] ) ;
#include <stdio.h>Void addition (int , int ) ;Void main (){Int a,b ;……....Addition (a,b) ;}Void addition (int m , int n){…...
1.5        Type_variable function_name  (parameter_name Type_variable  function_name  parameter_name

    #include<stdio.h>    #include<conio.h>    int calculate(int, int);    void main(void)    {    int p=3, q=4, r;    clr...
3     int calculate (int, int);                               argument       2       int                                  ...
1.6                      variable_name = function_name();      variable_name                             return (value)

1.7         (Local Variable)          (Global Variable)      (                       )
/* 7th Sample Program: Local vs Global Variable */#include<stdio.h>int ans = 0; int inc_one(int);                 /* func...
ans       (main)            ans                             a        inc_one         x     ans                  ans       ...
    PROGRAM AA;    VAR NUM:INTEGER;    PROCEDURE A;    BEGIN    NUM:=NUM*10;                     RUN    WRITELN(‘NUM*10 =...
- NUMoutput                                 output       program    A- NUM, cntr                 program       B          ...
2.                C                Header     #include
#include<stdio.h> #include<math.h>void main()                            Square root 4 =                                  ...
2.1               math.h                 double          double                          sin(), cos()  tan()
math.h                 double    double     sin(x)                                            sine       x      cos(x)    ...
2.2(string.h)•            string.h         strcpy                 :                        string 2            string 1   ...
strcmp       :   string 1              string 2             1   string 1   string 2         0                   strcmp str...
2.3                            #include <ctype.h>      TOLOWER            LOWER              TOLOWER(text)       text     ...
2         41.2.3.   -4.
–printf
1.          char s3[5] = {‘G’, ‘O’, ‘O’, ‘D’, ‘0’};   s3     ก)     ข)     ค)              .          .     ง)2.     ก)   ...
4.     ก)     ข)     ค)     ง)5.     ก)     ข)         ค)   {} []     ง)6.            char s1[9] = “LANGUAGE”;   s1     ก...
7.           char s2[4] = {‘G’, ‘O’, ‘O’, ‘D’};   s2     ก)     ข)     ค)              .          .     ง)8.     ก)   func...
10.  ก)   strcmp    strcmp string 1,string 2  ข)   strcat   strcat string 1,string 2  ค)   strcpy    strcpy string 1,strin...
1.2.3.4.5.6.7.8.9.10.
1.         132.          143.         194.          215.         226.          23     6/1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
Upcoming SlideShare
Loading in …5
×

ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1

296 views

Published on

Published in: Technology, Health & Medicine
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
296
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1

  1. 1. 6/1
  2. 2. (Function)
  3. 3. type int ,float , char , double , void void int function_name type parameter parameter , N parameter void { location variable declaration statement_ ; statement_ ; ... statement_N return(value);
  4. 4. F N T N INC U C IO S CS A D R TNAD PO RM E R G A MR LIBRARY DF E F N T N E IN D U C IO1. User-defined Function)2. Standard Function) include directives header file
  5. 5. 1.1.1type function_name(type1 arg1, type2 arg2,.., typeN argN) { local variable declaration; statement(s); return( varlue); }
  6. 6. type type int, float, chardouble int type voidfunction_nametype1 arg1, type2 arg2,…, typeN argN argument 1, 2, 3,…, N void{}local variable declaration localstatement(s) 1 ; (semicolon)return(value) value ()
  7. 7. #include<stdio.h>#include<conio.h>void one(void); /* function Prototypevoid two(void); /* function Prototypevoid main(void){clrscr();one(); /* call one() */two(); /* call two() */printf("nPress any key back to program ...");getch();}/* one function */void one(){int a=5, b=7;printf("A = %d, B = %dn",a,b);}/* two function */void two(){float p=4.5, q=3.5;p+=q;printf("P = %6.3f, Q = %6.3fn",p,q);}
  8. 8. 3 4 void one(void); void two(void); one( ) two( ) void one( )two( ) void () one( ) two argument main( ) 8 9 one( ); two( ); one( ) two( ) one( ) 14 18 two( ) 20 25 14 18 one( ) ab 20 25 two( ) p
  9. 9.    
  10. 10. 1.2 2#include <stdio.h> #include <stdio.h>Main () Main (){ {Function-name2 (); Function-name1 ();…………………... Function-name2Function-name1 (); ();} ………………….. }
  11. 11.  #include
  12. 12. 1.3 () void function_name(void )
  13. 13. #include<stdio.h>#include<conio.h>void asterisk_line(void);void main(void){clrscr( );asterisk_line( );printf("****** C and C++ PROGRAMMING ******n");asterisk_line();printf("nPress any key back to program ...");getch();}/* asterisk_line function */void asterisk_line( ){int j, n=40;for(j=1; j<=n; j++)printf("*");printf("n");}
  14. 14. 3 void asterisk_line (void); asterisk_line( ) 7 9 asterisk_line() 14 19 14 19 asterisk_line( ) * 40 10 11 enter
  15. 15. 1.4 void function_name (type_parameter parameter_name [,..] ) ;
  16. 16. #include <stdio.h>Void addition (int , int ) ;Void main (){Int a,b ;……....Addition (a,b) ;}Void addition (int m , int n){…............}
  17. 17. 1.5 Type_variable function_name (parameter_name Type_variable function_name parameter_name
  18. 18.
  19. 19.  #include<stdio.h> #include<conio.h> int calculate(int, int); void main(void) { int p=3, q=4, r; clrscr( ); r = calculate(p,q); printf("P = %d, Q = %d, R = %dn",p,q,r); printf("nPress any key back to program ..."); getch(); } /* end main() */ int calculate(int p, int q) { return (p+q); }
  20. 20. 3 int calculate (int, int); argument 2 int int caculate( ) 8 r = caculate (p, q); caculate( ) p q caculate( )13 16 13 16 caculate( ) p q main( ) r 9 p, q r
  21. 21. 1.6 variable_name = function_name(); variable_name return (value)
  22. 22.
  23. 23. 1.7 (Local Variable) (Global Variable) ( )
  24. 24. /* 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 %dn”, ans);}/* function definition: return x+1 */int inc_one(int x){ int ans; ans = x + 1; return ans;}
  25. 25. ans (main) ans a inc_one x ans ans ans inc_one ansinc_one ans
  26. 26.  PROGRAM AA; VAR NUM:INTEGER; PROCEDURE A; BEGIN NUM:=NUM*10; RUN WRITELN(‘NUM*10 = ‘,NUM); END; NUM*10 = 50 VAR ANY:INTEGER; PROCEDURE B; NUM = 124 VAR NUM,CNTR:INTEGER; BEGIN CNTR = 122 NUM:=124; BACK TO MAIN WRITELN(‘NUM = ‘,NUM); CNTR:=122; NUM = 50 WRITELN(‘CNTR =’,CNTR); END; BEGIN {MAIN PROGRAM} NUM:=5; WRITELN(‘IN MAIN’,’NUM = ‘,NUM); A; B; WRITELN(‘BACK TO MAIN NUM = ‘,NUM); END.
  27. 27. - NUMoutput output program A- NUM, cntr program B B NUM NUM NUM NUM=5 A 10 50 B 124 124 50 NUM- procedure B ANY procedure A procedure B- B A A B
  28. 28. 2. C Header #include
  29. 29. #include<stdio.h> #include<math.h>void main() Square root 4 = Square root 4 ={ 2.000000 2.000000 printf(“square root 4 =”); 5 power 3 = 5 power 3 = printf(“%fn”, sqrt(4)); 125.000000 125.000000 printf(“5 power 3 =”); printf(“%f”, pow(5,3));} include .h printf scanf include stdio.h sqrt pow include math.h
  30. 30. 2.1 math.h double double sin(), cos() tan()
  31. 31. math.h double double sin(x) sine x cos(x) cosine x (radian) tan(x) tangent x(radian) sqrt(x) x x exp(x) ex e 2.718282 pow(x,y) xy log(x) log e natural logarithm x log10(x) log 10 x ceil(x) x x
  32. 32. 2.2(string.h)• string.h strcpy : string 2 string 1 < strcpy ( , ) > strcpy string 1,string 2 strcat : string 2 string 1 strcat string 1,string 2
  33. 33. strcmp : string 1 string 2 1 string 1 string 2 0 strcmp string 1,string 2strlen : strlen stringstrcmpi : string 1 string 2 0 string 1 < string 2 <0 string 1 > string 2 >0 strcmpi string 1,string 2
  34. 34. 2.3 #include <ctype.h> TOLOWER LOWER TOLOWER(text) text TOUPPER UPPER LOWER TOUPPER TOUPPER(text)
  35. 35. 2 41.2.3. -4.
  36. 36. –printf
  37. 37. 1. char s3[5] = {‘G’, ‘O’, ‘O’, ‘D’, ‘0’}; s3 ก) ข) ค) . . ง)2. ก) ข) ค) . ง)3. ก) int function_name(type arg, …) ข) void function_name(type arg, …) ค) function_name(type arg, ….) ง) void function_name()
  38. 38. 4. ก) ข) ค) ง)5. ก) ข)  ค) {} [] ง)6. char s1[9] = “LANGUAGE”; s1 ก) ข) ค) . . ง)
  39. 39. 7. char s2[4] = {‘G’, ‘O’, ‘O’, ‘D’}; s2 ก) ข) ค) . . ง)8. ก) function_name(); ข) function_name() ค) function_name(arg1,arg2) ง) Function_name(arg1; arg2);9. ก) int function_name(type arg, …) ข) void function_name(type arg, …) ค) function_name(type arg, ….) ง) void function_name()
  40. 40. 10. ก) strcmp strcmp string 1,string 2 ข) strcat strcat string 1,string 2 ค) strcpy strcpy string 1,string 2 ง) strlen strlen string
  41. 41. 1.2.3.4.5.6.7.8.9.10.
  42. 42. 1. 132. 143. 194. 215. 226. 23 6/1

×