IfStmt1-B - How to use Prewritten C functions You've...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
How to use Prewritten C functions You've already been using a couple prewritten C functions (printf and scanf in the stdio library.) In general, each C function has a specification for how it is supposed to be called. The important parts of the specification are: 1) Name of the function 2) The type and order of each parameter the function takes 3) The return type of the function. For printf, the first parameter is a string, and the other parameters are optional parameters who's types correspond to the variables designated to print out by the printf statement. The return type is void, which means that the function returns nothing. Void functions are generally called on a line by themselves, as printf is. An useful function from the stdlib library is the rand function. This function takes in 0 parameters and returns a single integer. In particular, the integer returned is a random integer in between 0 and 32767 (2 15 - 1). For example, if you want to pick a random integer in between 1 and 100, you could do the following: int x = 1 + rand()%100; Any non-negative integer mod 100 will return a value in between 0 and 99 inclusive. Adding 1 to this yields an integer in between 1 and 100 inclusive.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
How to use C math library functions The rules for calling functions are the same regardless of which library they are in. Here are some common functions from the math library: // Returns the absolute value of x. int abs(int x); // Returns x y . double pow(double x, double y); // Returns the square root of x. double sqrt(double x); These functions can be called in the middle of arithmetic expressions, or be an arithmetic expression on their own. To call a function, substitute each parameter with an expression of the proper type, but do NOT actually type out that type. For example, if we wanted to assign a variable difference to the absolute value of the difference between to variables price and cost, we could do the following: difference = abs(price-cost); Here is an example that sets a double variable phi to 2 5 1 + . phi = (1+sqrt(5))/2; Here is another example that calculates the distance from the origin to a point (x,y). Assume x, y and distance are doubles: distance = sqrt(pow(x,2)+pow(y,2));
Background image of page 2
More about Using Math Functions Whenever you call a math function, just remember that each parameter can be ANY expression of the proper type. Most of the examples of the previous page are either variables or very simple expressions, but there's no reason a more complicated expression isn't possible. Also, do NOT put "int" or "double" in the parameter list when you CALL a function.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/09/2009 for the course COP 3223 taught by Professor Guha during the Fall '08 term at University of Central Florida.

Page1 / 13

IfStmt1-B - How to use Prewritten C functions You've...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online