Exercises : Answer the following Question

Submitted by Anonymous (not verified) on Sun, 03/10/2013 - 01:27

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix E, "Answers to Quiz Questions and Exercises."
Quiz

    Given the following function declarations, which ones are functions with a fixed number of arguments, which ones are functions with no arguments, and which ones are functions with a variable number of arguments?
        int function_1(int x, float y);
        void function_2(char *str);
        char *asctime(const struct tm *timeptr);
        int function_3(void);
        char function_4(char c, …);
        void function_5(void);
    Which one of the following two expressions is a function definition?

    int function_1(int x, int y);
    int function_2(int x, int y){return x+y;}

    What is the data type returned by a function when a type specifier is omitted?

    Of the following function declarations, which ones are illegal?
        double function_1(int x, ...);
        void function_2(int x, int y, ...);
        char function_3(...);
        int function_4(int, int, int, int);

Exercises

    Rewrite the program in Listing 15.2. This time use the format specifier %c, instead of %s, to print out the character string of the local time on your computer.
    Declare and define a function, called MultiTwo(), that can perform multiplication on two integer variables. Call the MultiTwo() function from the main() function and pass two integers to MultiTwo(). Then print out the result returned by the MultiTwo() function on the screen.
    Rewrite the program in Listing 15.3. This time, make a function that takes a variable number of int arguments and performs the operation of multiplication on these arguments.
    Rewrite the program in Listing 15.3 again. This time, print out all arguments passed to the AddDouble() function. Does va_arg() fetch each argument in the same order (that is, from left to right) of the argument list passed to AddDouble()?

Related Items

The goto Statement

The goto Statement

The continue Statement

The continue Statement

The break Statement

The break Statement

You can add a break statement at the end of the statement list following every case label, if you want to exit the switch construct after the statements within a selected case are executed.

The switch Statement

The switch Statement

Nested if Statements

Nested if Statements

As you saw in the previous sections, one if statement enables a program to make one decision. In many cases, a program has to make a series of decisions. To enable it to do so, you can use nested if statements.