Program name: library-function.c

Language features of C contained in "library-function.c"

Function:
  uses a "foreign" function that knows how to calculate square roots. Gives to the function a number, whose square root is desired. The function calculates and gives back that number's square root.

Source code:


Explanations: Here is another C program that, like the last one (user-defined-function.c), uses a function. The function is called sqrt and, given a number, calculates and gives back the number's square root. But this program does not contain the code itself for that function. The program "borrows" the function from somewhere else. The place it borrows from is called math.h.

Because square roots usually contain fractional values the variables for holding them cannot be integers. C offers a data type called "float" that understands and handles fractional numbers. Line 5 declares the variable "sq_root_of_three" which is intended to hold the square root of three. Anticipating that this won't be an integer, it declares the variable as having "float" type.

To do:

Key this program in, using a text editor. Save it in a file named "library-function.c". Then, in order to convert it into a form that contains correct Intel machine instructions, thus enabling the computer to run it, bring up the Windows command box and give this command:

bcc32 library-function.c

Alternatively use a browser to go to a C compiler web site. Cut your program's text from the text editor and paste it into the appropriate place in the web site. Tell the web site to compile (there's generally a button that says "compile" or maybe "run".)

If there are no error messages, a new file named "library-function.exe" will result. Then give the command:

library-function.exe

The program uses the function 3 times, to get the square roots of 3, 4, and then 5. What does it do with these square roots when it gets them? In the case of 3, it stores it in a variable (line 7). Then in prints the square root of 3 by printing that variable (line 9). In the other two cases, when it gets the square roots it prints them straightaway, without storing them anywhere (lines 10 and 11).