Next: Exercises Up: Introduction to User-defined functions Previous: Summary

Review Questions

  1. How is information supplied as input to a function? How can information be conveyed back to the calling program?

  2. What would the following function do?
    void example(int n)
      {
        int i;
        for (i=0; i<n; i++)
           cout << '*';
        cout << endl;
      }
    
    How would you call this function in a program? How would you use this function in producing the following output on the screen?
    *
    **
    ***
    ****
    

  3. What would be the output from the following programs?

    a)
    void change(void)
     {
       int x;
       x = 1;
     }
    void main()
     {
       int x;
       x = 0;
       change();
       cout << x << endl;
     }
    

    b)
    void change(int x)
      {
        x = 1;
      }
    void main()
      {
        int x;
        x = 0;
        change(x);
        cout << x << endl;
      }
    

  4. Write a function prototype for a function that takes two parameters of type float and returns true (1) if the first parameter is greater than the second and otherwise returns false (0).

  5. Write a function prototype for a function that takes two parameters of type int and returns true if these two integers are a valid value for a sum of money in pounds and pence. If not valid then false should be returned.

  6. A function named ex1 has a local variable named i and another function ex2 has a local variable named i. These two functions are used together with a main program which has a variable named i. Assuming that there are no other errors in the program will this program compile correctly? Will it execute correctly without any run-time errors?



Next: Exercises Up: Introduction to User-defined functions Previous: Summary