15

           18

      19

     31

       37

     38

           39

       6/2

1   2555
1


6


7


8


9
10


21


22
Mild Mapping
26


27
standard functions)

                                C                         h
eader   file      C                                 h
                                                 header file
                      #include<header file.h>


                                    C compiler



”              library functions)
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);




                                                              void)
                   return

}
1.                                                    User-defined
Function)



2.                                        Standard       Function)

                                 include directives         header
file


                          2

       1.
       2.


            #include <stdio.h>
            Main ()
            {
                 Function-name2 ();
                 …………………...
                 Function-name1 ();
            }



            #include <stdio.h>
            Main ()
            {
                 Function-name1 ();
Function-name2 ();
             …………………..
        }




1.
                                       #include

2.




                                  ()

                     void
     function_name(void )
void function_name
(type_parameter parameter_name [,..] ) ;

           function_ name

        type_parameter


        parameter_name
               1         ,




       #include <stdio.h>
       Void addition (int , int ) ;
       Void main ()
       {
       Int a,b ;
       ……....
Addition (a,b) ;
  }
  Void addition (int m , int n)
  {
  …............
  }




      Type_variable function_name parameter_name,… ;




Type_variable


function_name

parameter_name
variable_name = function_name();

variable_name




       return (value)
(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                            ans
                                                inc_one
                           ans
inc_one                                   ans
                                                    ans
#vinclude



2.1

      sinz x

                               :     sin       x    x
               Sin x   ;




      cos x

                           :       cossine      x   x
               cos x   ;




      tan x

                           :       tangent      x   x
               tan x   ;
sqrt x

                        :   2 square    root   x       x
  sqrt x    ;




pow x,y

                    :       x          y x         0
  pow x     ;




ceil x

                    :
   ceil x   ;




floor x

                    :
  floor x       ;




log x
:               log   n    x x   0
  log x     ;




sinz x

                                :              sin       x   x
         Sin x          ;




log10 x

                    :               log   10    x
  log10 x       ;




exp x

                            :                        e       e

    2.718282 x                                  e

         exp x          ;




fabs x
:                     x
             fabs x   ;




2.2

          strcpy

                                  :                 string2

m                      string1
                   Strcpy string1,

             string2



          strcat

                              :

string2                               string1

               strcat string1, string2



          strcmp

                          :

               Strcmp string1,

              string2
          strlen
:

          Strlen string



      strcmpi

                   :

-        string1                  string2        0

-        string1             < string2      <0

-        string1             > string2       >0


          Strcmp string1, strin

        g2
2.3

      tolower

                   :

        Tolower character

      toupper

                   :

        Toupper character
function_name


                -




                    -
-
function_name

type_parameter

parameter_name
tolower
toupper
strcmp

                                 rung




do

{

……………………….

} while (ans !=’n’) ;
-




-
printf

 sin   ()




#include
6

1.



 . type_variable function_name (parameter_name,…);

 . type_variable function_none (parameter_name,…);

 . variable function_name (parameter_name,…);

 . type_variable function_name (parameter_,…);

2.type_variable

 .

 .

 .

 .
3.function_name

 .

 .

 .

 .

4.                                                       1



 .     void

 .   void

 .

 .




5.



 . void function_name (type_parameter parameter_name[…]);

 . void function_name (type_parameter parameter_name);

 . void function_name (type_parameter parameter […]);
.

6.



 .

 .

 .

 .

7.



 .5

 .4

 .3

 .2

8.



 .
.



 .



 .




9.

 .



 .



 .

 .

10.



 .
.




 .




 .




1.   2.   3.   4.   5.   6.   7.   8.   9.   10.
.         7           C


                                http://e-


        learning.snru.ac.th/els/program lesson page _ html


                 21         2555.


                                               http://uhost.rmutp.ac.t

h/kriengkri.l/w3.pdf   18        2555

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

  • 1.
    15 18 19 31 37 38 39 6/2 1 2555
  • 3.
  • 4.
  • 5.
    standard functions) C h eader file C h header file #include<header file.h> C compiler ” library functions)
  • 6.
    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); void) return }
  • 7.
    1. User-defined Function) 2. Standard Function) include directives header file 2 1. 2. #include <stdio.h> Main () { Function-name2 (); …………………... Function-name1 (); } #include <stdio.h> Main () { Function-name1 ();
  • 8.
    Function-name2 (); ………………….. } 1. #include 2. () void function_name(void )
  • 9.
    void function_name (type_parameter parameter_name[,..] ) ; function_ name type_parameter parameter_name 1 , #include <stdio.h> Void addition (int , int ) ; Void main () { Int a,b ; ……....
  • 10.
    Addition (a,b) ; } Void addition (int m , int n) { …............ } Type_variable function_name parameter_name,… ; Type_variable function_name parameter_name
  • 11.
  • 12.
    (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;
  • 13.
    return ans; } ans (main) ans a inc_one x ans ans ans inc_one ans inc_one ans inc_one ans ans
  • 14.
    #vinclude 2.1 sinz x : sin x x Sin x ; cos x : cossine x x cos x ; tan x : tangent x x tan x ;
  • 15.
    sqrt x : 2 square root x x sqrt x ; pow x,y : x y x 0 pow x ; ceil x : ceil x ; floor x : floor x ; log x
  • 16.
    : log n x x 0 log x ; sinz x : sin x x Sin x ; log10 x : log 10 x log10 x ; exp x : e e 2.718282 x e exp x ; fabs x
  • 17.
    : x fabs x ; 2.2 strcpy : string2 m string1 Strcpy string1, string2 strcat : string2 string1 strcat string1, string2 strcmp : Strcmp string1, string2 strlen
  • 18.
    : Strlen string strcmpi : - string1 string2 0 - string1 < string2 <0 - string1 > string2 >0 Strcmp string1, strin g2 2.3 tolower : Tolower character toupper : Toupper character
  • 19.
  • 20.
  • 22.
  • 24.
  • 25.
  • 26.
    strcmp rung do { ………………………. } while (ans !=’n’) ;
  • 27.
  • 28.
    printf sin () #include
  • 29.
    6 1. . type_variablefunction_name (parameter_name,…); . type_variable function_none (parameter_name,…); . variable function_name (parameter_name,…); . type_variable function_name (parameter_,…); 2.type_variable . . . .
  • 30.
    3.function_name . . . . 4. 1 . void . void . . 5. . void function_name (type_parameter parameter_name[…]); . void function_name (type_parameter parameter_name); . void function_name (type_parameter parameter […]);
  • 31.
    . 6. . . . . 7. .5 .4 .3 .2 8. .
  • 32.
    . . . 9. . . . . 10. .
  • 33.
    . . . 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
  • 35.
    . 7 C http://e- learning.snru.ac.th/els/program lesson page _ html 21 2555. http://uhost.rmutp.ac.t h/kriengkri.l/w3.pdf 18 2555