ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
Upcoming SlideShare
Loading in...5
×
 

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

on

  • 356 views

 

Statistics

Views

Total Views
356
Views on SlideShare
355
Embed Views
1

Actions

Likes
0
Downloads
1
Comments
0

1 Embed 1

http://localhost 1

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

  • 6/1
  • (Function)
  • 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);
  • 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
  • 1.1.1type function_name(type1 arg1, type2 arg2,.., typeN argN) { local variable declaration; statement(s); return( varlue); }
  • 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 ()
  • #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);}
  • 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
  •    
  • 1.2 2#include <stdio.h> #include <stdio.h>Main () Main (){ {Function-name2 (); Function-name1 ();…………………... Function-name2Function-name1 (); ();} ………………….. }
  •  #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 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");}
  • 3 void asterisk_line (void); asterisk_line( ) 7 9 asterisk_line() 14 19 14 19 asterisk_line( ) * 40 10 11 enter
  • 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; 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); }
  • 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
  • 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); /* 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;}
  • ans (main) ans a inc_one x ans ans ans inc_one ansinc_one ans
  •  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.
  • - 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
  • 2. C Header #include
  • #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
  • 2.1 math.h double double sin(), cos() tan()
  • 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
  • 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
  • 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
  • 2.3 #include <ctype.h> TOLOWER LOWER TOLOWER(text) text TOUPPER UPPER LOWER TOUPPER TOUPPER(text)
  • 2 41.2.3. -4.
  • –printf
  • 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()
  • 4. ก) ข) ค) ง)5. ก) ข)  ค) {} [] ง)6. char s1[9] = “LANGUAGE”; s1 ก) ข) ค) . . ง)
  • 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()
  • 10. ก) strcmp strcmp string 1,string 2 ข) strcat strcat string 1,string 2 ค) strcpy strcpy string 1,string 2 ง) strlen strlen string
  • 1.2.3.4.5.6.7.8.9.10.
  • 1. 132. 143. 194. 215. 226. 23 6/1