Topical Information

Here are a few questions to help you clarify your understanding of the more advanced aspects of writing your own functions — inline functions, defaulted arguments, assertions, and overloaded functions.

Question Set Information

Questions

  1. Code a function which will add one number to another number. Have the second number be 5 by default. return the numbers' sum.

    
            Prototype:
    
                long add_em(short number1, short number2 = 5);
    
            Definition:
    
                long add_em(short number1, short number2)
                {
                    return number1 + number2;
                }
    
    

    Show a few alternative calls to your function (and all necessary supporting code). (Hint: Some should use the default value and some should not.)

    
            add_em(45)         // should return 50
    
            add_em(45, 6)      // should return 51
    
    
  2. To make a function execute faster, we can place the keyword inline before its head. This is only, however, a suggestion to the compiler which gets the final say in whether this function can speed up by simply replacing a call to it with its definition. Because of how this rule works, such functions do not typically have a prototype/declaration (since the entire definition [sometimes] needs to be seen before the call — not just the prototype as we normally code).

    This procedure should only be done to functions which are fairly small/simple (like the ones we used in algebra). Not ones with complicated code (such as from the end of Chapter 3).

    Such functions placed in a library are an exception to the rule of not placing code in a library interface file.

  3.  
    TRUE FALSE  Functions can have arguments which may be present or not.
    TRUE FALSE  Such arguments are known as inline arguments.
    TRUE FALSE  These must be the first arguments in the list.
    TRUE FALSE  The caller wishing to use the default value can leave that argument value out of the call.
    TRUE FALSE  These arguments are typically 'by-value' arguments since we do not make global variables to which they might refer.
  4. What is different about two functions which are overloaded?

    
            For two functions to be overloaded, they need to have either a
            different number of arguments or different types of arguments so that
            the compiler can tell from the call which function to actually call.
    
    

    What is the same about two functions which are overloaded?

    
            They must have the same name to be overloads of one another.
    
    

    What do we expect to be similar (but is not necessarily) about two functions which are overloaded?

    
            If functions have the same name, we expect that they'll perform the
            same task — or at least a very similar task.
    
    
  5. Write two overloaded functions which each print a value supplied by their caller. Both should insert the caller's value into the sentence: "Your lucky number is ______.".

    One should print a floating point number and the other should print a whole number. (Hint: What data type should the whole function accept?)

    
            void print_lucky(double number)
            {
                cout << "Your lucky number is " << number << ".\n";
                return;
            }
    
            void print_lucky(long number)
            {
                cout << "Your lucky number is " << number << ".\n";
                return;
            }
    
    

    Show calls to both the above functions (with any supporting code necessary).

    
            print_lucky(4.2);
    
            print_lucky(4L);
    
    
  6. The assert macro from our C heritage will allow you to test for certain Boolean conditions at run-time and crash the program should they not be met. This is handy during the debugging stage of development but not so good during production phase. To avoid crashes during a production run, we just need to #define the macro NDEBUG to turn all the Boolean tests off.