Phr0zen_Penguin

Default Function Arguments

Oct 15th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. /*==================================================================================================
  2.   --------------------------------------------------------------------------------------------------
  3.   [defFuncArgument.cpp]
  4.   Default Function Arguments.
  5.  
  6.         (c) Damion 'Phr0z3n.dev' Tapper, 2014.
  7.         Email: [email protected]
  8.   --------------------------------------------------------------------------------------------------
  9.   ==================================================================================================*/
  10.  
  11. #include <iostream>
  12.  
  13. /*===============================================--=================================================
  14.                                     THE defFunc METHOD PROTOTYPE
  15.   --------------------------------------------------------------------------------------------------*/
  16. int defFunc(int firstParam, int secParam = 10);
  17. /**
  18.  * The second parameter (secParam) is initialized to 10 by default.
  19.  *
  20.  * NOTE:
  21.  * This method can ONLY be used either in the prototype (declaration), in which case, the names of the
  22.  * parameters MUST be stated (that is, they cannot be left off by just stating the parameter types);
  23.  * or, in the initial method/function definition, when the method/function itself is defined first,
  24.  * without a prototype, before its use.
  25.  *
  26.  * Also note that, in the former scenario, the default initialization of any parameter can only be done
  27.  * once, i.e. in the declaration of the method/function prototype (and in no way, alternatively).
  28.  **/
  29.  
  30. /*===============================================--=================================================
  31.                                             THE main METHOD
  32.   --------------------------------------------------------------------------------------------------*/
  33. int main(void)
  34. {
  35.     /**
  36.      * A call is made to defFunc providing only one parameter.
  37.      * The initializing of the second parameter (to 10) safeguards against your code throwing a
  38.      * 'Division by Zero' exception, for instance, in a case where only one (the first) argument is
  39.      * provided during a call to the method/function.
  40.      *
  41.      * NOTE:
  42.      * All parameters in the prototype/method/function MUST initialized after the ones (to be assumed)
  43.      * provided (as arguments) during a call to the method/function,
  44.      * i.e. no default initialization can be omitted.
  45.      *
  46.      * Example, a method with parameters int defFunc(int firstParam = 5, int secParam, int thirdParam = 2),
  47.      * will cause a compile error if it is called with defFunc(10), for instance.  The compiler always
  48.      * assumes that each argument provided must be passed in chronological order (from left to right).
  49.      *
  50.      * If defFunc() had ALL parameters initialized to default values:
  51.      * int defFunc(int firstParam = 5, int secParam = 10, int thirdParam = 2), for instance, a call such
  52.      * as defunc() is also valid.
  53.      *
  54.      * Finally, this method of omitting initially initialized (default) method/function parameters is
  55.      * completely illegal (in all senses) when a method/function is overloaded.
  56.      **/
  57.     std::cout << defFunc(20) << std::endl;
  58.  
  59.     /**
  60.      * Another call is made to defFunc() with all the arguments provided.
  61.      **/
  62.     std::cout << defFunc(2, 5) << std::endl;
  63.  
  64.     return 0;
  65. }
  66.  
  67. /*===============================================--=================================================
  68.                                         THE defFunc METHOD
  69.   --------------------------------------------------------------------------------------------------*/
  70. int defFunc(int firstParam, int secParam)
  71. {
  72.     return (firstParam * secParam);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment