Functions in c language

 

What is  a  function  in C  language ?

   A   function  is  a  group  of   statements  that  perform  together  a  specific  task(subprogram).

   Any  C  program  consists  of  one  or  more  functions. If  a  program  has  only  one  functions 

   then  it  must  be  the  main()  function. It  is  the  first  function  to  be  executed  when  the 

   program  starts, all  other  functions  are  called  directly  or  indirectly  from  main().

   


 
 C  program  have  two  types  of  functions-

        1.  Library  functions 

        2.  User   define  functions 

1.  Library  functions(built in function):-   C  has  the  facility  to  provide  library  functions

          for  performing   some  operations. These  functions  are  present  in  the  C  library  and 

          they  are  predefined. For  example  sqrt ()  is  a   mathematical   library  function  which

          is  used  for  finding  out  the  square  root  of  the  any  number. The  functions  scanf (),

          printf () are input  output  library  functions.

          To  use  a  library  function  we  have  to  include  corresponding  header  file  using  the  

          preprocessor  directive  #include. For  example  to  use  input  output  functions  like  

          printf ()  and  scanf ()  we  have  to  include  stdio.h , for  mathematical  library  functions  

          we  have  to  include  math.h, for string  library  functions  string.h should  be  included .

2.  User  define  functions:-  A  user  define  function  is  a  function  that   provided  by  user  of  

           a  program. These  types  of  function  are  called  user  defined  functions.  To  create  and 

           use  these   functions.  We  should  know  about  these  three  things-

1. Function  definition

2. Function  declaration

3. Function  call

  1. Function  definition:-  The  function  definition  consists  of  the  whole  description  and  

           code  of  a  function .  It  tells  what  the  function  is  doing  and  what  is  its  input  and 

           output. A  function  definition  consists  of  two  parts - a  function  header  and  a  function

           body. The  general  syntax  of  a  function  definition  is -

return_ type  function_ name(parameter  declarations)

2.  Function  declaration:- A  function  declaration  is  used  to  give  specific  information  to  the 

           compiler  about  the   function  so  that  it  can  check  the  function  calls. The  calling   

           function  needs  information  about  the  called  function.

3. Function  call  or  Function  invocation:- The  function  definition  describes  what  a  function 

            can  do, but  to  actually  use  it  in  program  the  function  should  be  called  somewhere.

            A  function  is  called  by  simply  writing  its  name .

   For  example :

            int sum (int x , int y)

        {

            int  x, y, s;

            s = x + y;  //  function  call    

            return s;

        }

           

Post a Comment

Comment from message

Previous Post Next Post