Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*==================================================================================================
- --------------------------------------------------------------------------------------------------
- [defFuncArgument.cpp]
- Default Function Arguments.
- (c) Damion 'Phr0z3n.dev' Tapper, 2014.
- Email: [email protected]
- --------------------------------------------------------------------------------------------------
- ==================================================================================================*/
- #include <iostream>
- /*===============================================--=================================================
- THE defFunc METHOD PROTOTYPE
- --------------------------------------------------------------------------------------------------*/
- int defFunc(int firstParam, int secParam = 10);
- /**
- * The second parameter (secParam) is initialized to 10 by default.
- *
- * NOTE:
- * This method can ONLY be used either in the prototype (declaration), in which case, the names of the
- * parameters MUST be stated (that is, they cannot be left off by just stating the parameter types);
- * or, in the initial method/function definition, when the method/function itself is defined first,
- * without a prototype, before its use.
- *
- * Also note that, in the former scenario, the default initialization of any parameter can only be done
- * once, i.e. in the declaration of the method/function prototype (and in no way, alternatively).
- **/
- /*===============================================--=================================================
- THE main METHOD
- --------------------------------------------------------------------------------------------------*/
- int main(void)
- {
- /**
- * A call is made to defFunc providing only one parameter.
- * The initializing of the second parameter (to 10) safeguards against your code throwing a
- * 'Division by Zero' exception, for instance, in a case where only one (the first) argument is
- * provided during a call to the method/function.
- *
- * NOTE:
- * All parameters in the prototype/method/function MUST initialized after the ones (to be assumed)
- * provided (as arguments) during a call to the method/function,
- * i.e. no default initialization can be omitted.
- *
- * Example, a method with parameters int defFunc(int firstParam = 5, int secParam, int thirdParam = 2),
- * will cause a compile error if it is called with defFunc(10), for instance. The compiler always
- * assumes that each argument provided must be passed in chronological order (from left to right).
- *
- * If defFunc() had ALL parameters initialized to default values:
- * int defFunc(int firstParam = 5, int secParam = 10, int thirdParam = 2), for instance, a call such
- * as defunc() is also valid.
- *
- * Finally, this method of omitting initially initialized (default) method/function parameters is
- * completely illegal (in all senses) when a method/function is overloaded.
- **/
- std::cout << defFunc(20) << std::endl;
- /**
- * Another call is made to defFunc() with all the arguments provided.
- **/
- std::cout << defFunc(2, 5) << std::endl;
- return 0;
- }
- /*===============================================--=================================================
- THE defFunc METHOD
- --------------------------------------------------------------------------------------------------*/
- int defFunc(int firstParam, int secParam)
- {
- return (firstParam * secParam);
- }
Advertisement
Add Comment
Please, Sign In to add comment