Functions

Sunday 14 August 2016

Functions:

  • Function is a collection of statements that perform some operations.
         example:  main(),printf(),clrscr(),scanf(),..etc are functions.
  • Ever c program must contain main(),it is not posible to run a program with out main().
  • Although it can be compiled we can only define main() but we cannot call main(),os calls main() as soon as you run the program and main in turn calls other functions.
  • Define main() any where in the program execution always starts at main() irrespective of where it is defined.

Types of functions:

  1. Pre defined functions 
  2. User defined functions
Pre-defined functions:
  • the functions which have already been written ,compiled and placed in libraries are called pre-defined functions or library functions or built in functions.
         Example: printf(),scanf(),cos(x),sqrt(x),..etc
  • user or programmer directly use predefined functions with out writing a code.
  • function definition and prototype are substituted when .h file is included.
  • C language support set of pre defined functions they are input/output functions,String functions,math functions,Character functions.
User defined functions:
  • The functions defined by the users according to their requirment are called user defined functions.
  • exmple: main()

concepts associated with user defined functions are:

function declaration:

  • A function declaration provides the following information to the compiler.
  • the name of the function , the type of the value returned and the information about the type of arguments.
       Syntax:  
             return-type  function-name  (datatype parameter1,datatype parameter2......);

                                int  addition(int x,int y);
Function  definition:
  • writing  code in the function is defining the function, we inform to the system what function does through function definition.
      
           Syntax:
         

function definition consists of two parts 
  • function header  
  • function body

function header:

  • first line in function definition is a function header,it is similar to function declaration
  • it specifies function return type ,function name,formal parameters adn types to the compiler.
syntax:  return-type   function-name (parameter list)

function body:

  • collection of statements eclosed with in braces is called body of function.
  • example:         
                               {
                                    int x,y,z;
                                    x=1;
                                    y=2;
                                    z=x+y;
                                   return z;
                                }
                                   

function parameters:

  • function parameters are used to provide comminication among functions.
  • function parameters are used to pass data between two  functions.
  • function may have 0 or 1 or more parameters.
Types of parameters:
Actual parameters: 
  • the parameters which are included in function call refered as actural parameters.
                 example: sum(a,b);
          Here a,b  are called actural parameters.
  • calling function transfer the values of actual parameters to the called function.
Formal parameters:
  • The parameters which are included in function header refered as formal parameters.
      Example: int sum(int x,int y)
  • formal parameters receive data from calling function.

Function call:

  • The function can be called by simply specifying the name of the function,return value and parameters is optional.
Syntax:     function-name();
                funtion-name(parameters);
                return-value=function-name(parameters);

return statement:

  • return is a keyword in c language.
  • return statement transfer flow of control from called function to calling function.
  • if function contain no return statement then return type is void.
  • the value returned by the return statement is returned to function call.

Over view of functions:



   
you can also refer:    
-----------------------------------------------------------------------------------------------------


No comments:

Post a Comment