## CPSC 124, Fall 2001 Quiz #4, October 15

This is the third quiz in CPSC 124: Introductory Programming.

Question 1: For this question, you should write a complete Java subroutine. The name of the subroutine is pythagoras. It has a return value of type double, (so it is really a function). It has two parameters of type double named x and y. The purpose of the function is to compute and return the value of the mathematical expression sqrt(x2+y2), where sqrt is the square root function.

```            static double pythagoras( double x, double y ) {

return Math.sqrt( x*x + y*y );

}
```

(The "static" is optional, but makes sense here since this subroutine seems to stand on its own, rather than being part of an object. It's possible to write this in more lines, but unnecessary.)

Question 2: Show how you would use the subroutine described in problem 1) to compute sqrt(32+52).

```             pythagoras(3,5)
```

(This is a minimal correct answer. But this does not show how the result returned by the function is to be used. In an actual program, the result would be used in some way. For example, it could be assigned to a variable:  "answer = pythagoras(3,5);".  Or it might be printed out:  System.out.println("The length of the hypotenuse is " + pythagoras(3,5));

Note that this function would never be called with a statement such as "pythagoras(3,5);" since it doesn't make sense to call this function unless you do something with the return value.)

Question 3: Briefly explain how subroutines can be useful in the bottom-up design of programs and what this has to do with software toolkits.