Chapter 3: routines
Questions
 

1.   Describe the format of
•    a procedure
•    a function
 
2.   Describe the signature of
•     an attribute
•     a procedure
•     a function
 
3.    Can a constant be a local variable? Why was Eiffel designed like that?

4.    How is a routine called? What happens when a routine is called?

5.    How is data passed to a routine from its caller? How is data returned by a routine?

6.    Describe the format of an argument list. Define what is meant by
•     actual argument
•     formal argument
•     argument binding

7.    How is a local variable declared? What is the scope of a local variable? What is the scope of an attribute? Can a local variable have the same name as an attribute of the class? Can a local constant be declared?

8.    Is Result a local variable? What is the initial value of Result in a function? Can the value of Result be used inside a function?

9.    What type of routine is used to read from a user? Why? What type of routine is used to show data to the user? Why?

10.  What is a side effect? Why are side-effects hard to debug? Why are side effects bad for reuse?
 
11.  When should you use a function, and when a procedure?

12.  Can a function call a function? Can a function call a procedure? Can a procedure call a function? Can a procedure call a procedure?

13.  What are the values of the formal arguments in the following example?

        local a, b: REAL
        do
            a := 12.6
            do_it (a, a + 32)
            do_it (b, a)
            do_it (a, b)
        end --

     do_it (b, c: REAL) is ...

14.    The routine do_it is defined as

        do_it (b, c: REAL) is
                        -- do it
            do
                        gron := gron + b - c * 2
            end -- do_it
where gron: REAL is an attribute. If gron has the initial value 0, what is the value of gron on return from each procedure call in question 12?

15.    What values are returned by each call to the routine wonder below?

        wonder (about, this: REAL): REAL is
                        -- wonder what this does?
            local hero: INTEGER
            do
                        hero := about.truncated_to_integer + 4
                        Result := hero * this
            end -- wonder

        wonder (3, 4)
        wonder (96.2, 17.8)
        wonder (1, 2, 3)
        wonder ("about", "this")
        wonder (sqrt (3), abs (-12))

16.    This is an exercise on syntax and mechanism; do not worry about style.
The class X has a REAL attribute called number, a make routine, and four other routines. The first routine (set) receives the initial value of number as an argument. The second routine (add3) adds 3 to the number and displays the new value. The third routine (add) takes an integer as argument, adds this to number, and displays the new value. The fourth routine takes an integer and two strings, and displays the first string, then the sum of number and the integer, then the second string.
        Hint: Code a routine header, then the routine body.

17.     This is an exercise on syntax and mechanism; do not worry about style.
Add two new features to class X. The first feature (square_num) returns the square of number. The second feature (formula) accepts two integer values and a real value (call the arguments i, j, a) and returns the value ((number + i) / j) * a. Display the returned value.
        Hint: Code a routine header, then the routine body.

18.    Code a solution for the specification given in Chapter 2, question 14, using routines.

19.    Code a solution for the specification given in Chapter 2, question 15, using routines.

20.    Code a solution for the specification given in Chapter 2, question 17, using routines.