Page 68 - DCAP404 _Object Oriented Programming
P. 68

Unit 3: Review of Functions




          float  b;                                                                             Notes
          cout  <<“  Enter  a  number”;
          cin  >>  b;
          a = b * b;            //instead  of  square(b);  .
                         cout  <<  “\nThe  square  of  “  <<  b  <<  “is  “  <<  a;
          }
          Since, the code will be coded wherever it is called, the program size increases and hence requires
          more memory space.





             Notes  However, because no function calling overhead is involved, execution becomes
             fast.

          Self Assessment

          Fill in the blanks:

          11.  Inline functions are functions where the ……………. is made to inline functions.
          12.  The inline function takes the format as a …………………….. but when it is compiled it is
               compiled as inline code.

          3.4 Default Arguments


          C++ functions can have arguments having default values. The default values are given in the
          function prototype declaration. Whenever a call is made to a function without specifying an
          argument, the program automatically assigns values to the parameters as specified in the default
          function prototype declaration.
          Default arguments facility allows easy development and maintenance of programs .To establish
          a  default value you have  to use the function  prototype. Because the compiler looks at  the
          prototype to see how many arguments a function uses, the function prototype also has to alert
          the program  to the possibility of default arguments. The method is to assign a value to the
          argument in the prototype. The following program explains the use of default arguments:

          #include  <iostream.h>
          void  rchar  (char  ch  =  ‘*’,  int  num  =  10);
          //default  arguments  with  values  ‘*’  and  10
          void  main  (void)
          {
          rchar  (  );
                         rchar  (‘=’);

          rchar  (‘+’,  30);
          }
                 void  rchar  (char  ch,  int  num)
          {



                                           LOVELY PROFESSIONAL UNIVERSITY                                   61
   63   64   65   66   67   68   69   70   71   72   73