CompuServe Messages

#C subroutines?

    31-Jul-94 18:36:00
Sb: #42039-#C subroutines?
Fm: Bill Roberts 76056,1613
To: Peter Greci 76104,2110
Subroutines are not only possible in C, they are encouraged. Sample subroutine to calculate the hypotinuse of a right triangle: float hypot( float a, float b) { return sqrt(a*a + b*b); } This sample conforms to the ANSI C standard, and may not be familure to those who know only the K&R (the old standard). The first line tells the compiler to create a subroutine called hypot which will receive two floating point numbers and return a floating point number. The instructions for the subroutine are between the brackets. The only instruction (this is a very simple instruction) says to return the result of the square root of etc… You purists will say "Ah, but sqrt takes a double and returns a double." Correct, and that is why I used the ANSI definition. The compile will take care of all the conversion for me. To use the function, in the main program you would say something like printf( "The three sides of the triangle are %9.4f, %9.4f, %9.4f\n", x, 12.2, hypot( x, 12.2 ) ); Actually, and introductory C manual should have at least one chapter devoted to function. C you around. Bill Roberts — Autopilot from Ellettsville, Indiana