Page 70 - DCAP404 _Object Oriented Programming
P. 70

Unit 3: Review of Functions




                  return 0;                                                                     Notes
              }
               int  f(int  length,  int  width,  int  height)
              {
                       return  (length  *  width  *  height);
              }

          3.5 Function Prototyping

          Prototype of a function is the function without its body. The C++ compiler needs to about a
          function before you call it, and you can let the compiler know about a function is two ways – by
          defining it before you call it or by specifying the function prototypes before you call it. Many
          programmers prefer a ‘Top-Down’ approach in which main() appears ahead the user-defined
          function definition. In such  approach, function access  will precede  function definition. To
          overcome this, we use the function prototypes or we declare the function. Function-prototype
          are usually written at the beginning of a program, ahead of any programmer-defined functions
          including main(). In general, function prototype is written as:
             Return-type  name  (type  1  arg.  1,  type  2  arg.  2,  …..  type  n  arg.  n);
          Where return-type represents the data type of the value returned by the function, name represents
          the function name, type 1, type 2 …. type n represents the data-type of arguments art. 1, arg. 2 …
          arg n. The function prototypes resemble first line to function definition, though it ends with the
          semicolon. Within the function declaration, the names of arguments are optional but data-types
          are necessary for eq. Int count (int);

          The code snippet given below will cause compilation error because the main function does not
          know about the called function one().
          void  main()

          {
                 …
                 one();         //incorrect!!  No  function  prototype  available  for  one()
                 …
          }
          void  one()
          {
                 //function  definition
          }
          To resolve this problem you should define the function before it is called as shown below:

          void  one()
          {
                 //function  definition
          }
          void  main()





                                           LOVELY PROFESSIONAL UNIVERSITY                                   63
   65   66   67   68   69   70   71   72   73   74   75