Sams Teach Yourself C in 21 Days

(singke) #1
Answers 837

F



  1. The first line of a function definition must be the function header. It contains the
    function’s name, its return type, and its parameter list.

  2. A function can return either one value or no values. The value can be of any of the
    C variable types. On Day 18, “Getting More from Functions,” you’ll see how to get
    more values back from a function.

  3. A function that returns nothing should be type void.

  4. A function definition is the complete function, including the header and all the
    function’s statements. The definition determines what actions take place when the
    function executes. The prototype is a single line, identical to the function header,
    but it ends with a semicolon. The prototype informs the compiler of the function’s
    name, return type, and parameter list.

  5. A local variable is declared within a function.

  6. Local variables are independent from other variables in the program.
    10.main()should be the first function in your listing.


Exercises

1.float do_it(char a, char b, char c)
Add a semicolon to the end, and you have the function prototype. As a function
header, it should be followed by the function’s statements enclosed in braces.
2.void print_a_number( int a_number )
This is a voidfunction. As in exercise 1, to create the prototype, add a semicolon
to the end. In an actual program, the header is followed by the function’s state-
ments.


  1. a.int
    b.long

  2. There are two problems. First, the print_msg()function is declared as a void;
    however, it returns a value. The returnstatement should be removed. The second
    problem is on the fifth line. The call to print_msg()passes a parameter (a string).
    The prototype states that this function has a voidparameter list and, therefore,
    shouldn’t be passed anything. The following is the corrected listing:
    #include <stdio.h>
    void print_msg (void);
    int main( void )
    {
    print_msg();
    return 0;
    }


49 448201x-APP F 8/13/02 11:22 AM Page 837

Free download pdf