FUNCTIONS

QR Code 2d Barcode Decoder In NoneUsing Barcode Control SDK for Software Control to generate, create, read, scan barcode image in Software applications.

QR Code JIS X 0510 Printer In NoneUsing Barcode generation for Software Control to generate, create QR-Code image in Software applications.

[CHAP. 4

QR Code Scanner In NoneUsing Barcode scanner for Software Control to read, scan read, scan image in Software applications.

Create QR Code ISO/IEC18004 In C#Using Barcode printer for VS .NET Control to generate, create Denso QR Bar Code image in Visual Studio .NET applications.

This program prints the square roots of the numbers 0 through 5. Each time the expression sqr t ( i > is evaluated in the for loop, the sqr t function is executed. Its actual code is hidden away within the Standard C Library. In using it, we may confidently assume that the expression sqrt ( i ) will be replaced by the actual square root of whatever value i has at that moment. Notice the directive # inc lude <math . h> on the second line. This is necessary for the compiler to find the definition of the sqr t function. It tells the compiler that the function is declared in the <math. h> header file.

Denso QR Bar Code Creator In .NET FrameworkUsing Barcode generator for ASP.NET Control to generate, create QR Code JIS X 0510 image in ASP.NET applications.

Draw QR In Visual Studio .NETUsing Barcode drawer for .NET Control to generate, create Denso QR Bar Code image in VS .NET applications.

A function like sqr t ( ) is executed by using its name as a variable in a statement, like this:

Make QR In Visual Basic .NETUsing Barcode drawer for .NET framework Control to generate, create QR Code image in .NET applications.

Making EAN / UCC - 13 In NoneUsing Barcode maker for Software Control to generate, create GS1 - 13 image in Software applications.

y = sqrt(x);

Encode Bar Code In NoneUsing Barcode drawer for Software Control to generate, create bar code image in Software applications.

Code 128A Drawer In NoneUsing Barcode generation for Software Control to generate, create Code128 image in Software applications.

This is called invoking or calling the function. Thus in Example 4.1, the code sqrt ( i ) caZZs the sqrt function. The expression i in the parentheses is called the argument or actual parameter of the function call, and we say that it is passed by vaZue to the function. So when i is 3, the value 3 is passed to the sqr t function by the call sqr t ( i ) . This process is illustrated by the following diagram:

Code 39 Full ASCII Printer In NoneUsing Barcode encoder for Software Control to generate, create Code 39 Full ASCII image in Software applications.

Barcode Encoder In NoneUsing Barcode creation for Software Control to generate, create barcode image in Software applications.

main0 3

Printing USPS OneCode Solution Barcode In NoneUsing Barcode creation for Software Control to generate, create USPS Intelligent Mail image in Software applications.

Creating Data Matrix ECC200 In NoneUsing Barcode encoder for Font Control to generate, create DataMatrix image in Font applications.

1.73205 /

Code 128 Code Set A Generation In Objective-CUsing Barcode generator for iPad Control to generate, create Code 128C image in iPad applications.

Bar Code Printer In NoneUsing Barcode generation for Microsoft Word Control to generate, create bar code image in Office Word applications.

sqrt()

Draw European Article Number 13 In NoneUsing Barcode creation for Font Control to generate, create EAN 13 image in Font applications.

USS Code 39 Encoder In JavaUsing Barcode printer for Android Control to generate, create Code39 image in Android applications.

The variable i is declared in main ( ) . During the fourth iteration of the for loop, its value is 3. That value is passed to the sqrt ( ) function which then returns the value 1.73205.

Data Matrix Creation In .NET FrameworkUsing Barcode maker for .NET Control to generate, create Data Matrix image in VS .NET applications.

Data Matrix 2d Barcode Printer In JavaUsing Barcode encoder for Android Control to generate, create Data Matrix 2d barcode image in Android applications.

EXAMPLE 4.2 Testing an Identity from Trigonometry

Here is another program that uses the <math . h> header file. Its purpose is to allow an empirical verification of the standard trigonometric identity sir-h = 2 sinxcosx :

#include <iostream.h> #include <math.h> // Program to test trigonometric identity sin 2x = 2 sin x cos x: main0 for (float x = 0; x < 2; x += 0.2) tout << "\t" << x << "\t\t" << sin(2*x) cc 2*sin(x)*cos(x) C-C endl;

0 O-0

<< "\t"

0*389418

0.9 32039 . -. o-6 oi ,0.999574 1 0.909297 f*2 O,Q754G3 1.4 0 t J34988 I..6 . . -0Jl583744 1.8 -0.442521

0*389428 0.717356 0.932039, 0 * 999574 0,909297 0.675463 01334988 -0,0583744 -0.442521

. . .:. . ..j . ..

The program prints x in the first column, sin 2x in the second column, and 2 sin x cos x in the third column. For each value of x tested, sin 2x = 2 sin x cos X. Of course, this does not prove the identity. It merely provides convincing empirical evidence of its truth.

CHAP. 41

FUNCTIONS

Function values may be used like ordinary variables in an expression. Thus we can write

y = sqrt(2); tout << 2*sin(x)*cos(x);

We can even nest function calls, like this: Y = sqrt(1 + 2*sqrt(3 + 4*sqrt(5))) Most of the mathematical functions that you find on a pocket calculator are declared in the <math . h> header file, including all those shown in Table 4.1. Table 4.1 Some <math. h> Functions Function

aces(x) asin atan ceil(x) cos (x) exp (x> fabs(x) floor(x) log (x> loglO POWkP) sin(x) sqrt (x> tan(x)

Description

inverse cosine of x (in radians) inverse sine of x (in radians) inverse tangent of x (in radians) ceiling of x (rounds up) cosine of x (in radians) exponential of x (base e) absolute value of x floor of x (rounds down) natural logarithm of x (base e) common logarithm of x (base 10) x to the power p sine of x (in radians) square root of x tangent of x (in radians)

Example

acos(0.2) returns 1.36944 asin(0.2) returns 0.201358 atan(0.2) returns 0.197396 ceil(3.141593) returns

cos (2) returns -0.416147 exp(2) returns 7.38906 fabs (-2) returns 2.0 floor(3.141593) returns 3.0 log(2) returns 0.693147 loglO(2) pow(2,3) returns 0.30103 returns 8.0

sin(2) returns 0.909297 sqrt(2) returns 1.41421 tan(2) returns -2.18504

Notice that every mathematical function returns a double type. If it is passed an integer, the int is promoted to a double before it is processed by the function. Table 4.2 lists some of the more useful header files in the Standard C Library. Table 4.2 Some of the Header Files in the Standard C Library Header File