1 / 15

More about Math Functions (preview version)

More about Math Functions (preview version). The following functions are all in double data type fabs(x) returns |x| fabs(1.45)= 1.45 fabs(-1.45)= 1.45 sqrt(x) returns the square root of x sqrt(25) = pow(x, y) returns x y pow(2,3) =

gray-gould
Download Presentation

More about Math Functions (preview version)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More about Math Functions (preview version) The following functions are all in double data type • fabs(x) returns |x| fabs(1.45)= 1.45 fabs(-1.45)= 1.45 • sqrt(x) returns the square root of x sqrt(25) = • pow(x, y) returns x y pow(2,3) = • floor(x) returns the largest integer less or equal to x floor (4.15) = floor (-4.15)= • ceil(x) returns the smallest integer greater than or equal to x ceil (4.15) = ceil (-4.15) =

  2. Trigonometric Functions • sin (x), cos (x), tan (x) • Argument should be in radians, not degrees! • Converting from Degrees to Radians • 0 degrees is 0 radians • 360 degrees is 2 radians • In general, if x is in degrees, to convert to radians, multiply by ( / 180.0) if theta_deg is angles in degrees, theta_rad = theta_deg * 3.14159 / 180.0;

  3. User-Defined Functions • Function • A named independent section of code that performs a specific task and optionally returns a value to the calling program. • Input arguments: used to pass information into a function • Output arguments: returns result to a calling program • Function prototype • Provide compiler with description of a function that will be defined in the later point of the program. It defines the return types of output argument and types of input arguments, as well as the function name. • Should be put before the function call • Syntax: ftype fname(list of types of input arguments);

  4. User-Defined Functions (cont’d) • Function definition • Is the actual function; it contains the code that will be executed. • Function header: similar to the prototype but has specified names for input parameter (a placeholder) • Function body is enclosed in {..} immediately follows the function header. • It contains local variable__declared in the function, won’t affect variables outside the function, even with the same name. Syntax: ftype fname(formal parameters){ local variable declaration statements } • Function call • To activate a function by a program. When called, the program can send the function information in forms of actual arguments • Syntax: fname(actual argument)

  5. Function with Input Arguments and One Result

  6. Functions find_circum and find_area

  7. Effect of Executing circum = find_circum (radius);

  8. Memory allocation with function and function call main function Area = find_area(radius) Circum = find_circum(radius) • When find_area( radius ) is called • It copies the value of radius to temporary memory locations r • Then calculate PI* r*r and store the result in a templocation result find_area( ) find_circum( ) radius area circum r result

  9. User-Defined Functions (cont’d) • Void function__ a function does not return a value • Syntax: void fname(…){ … } • Void argument • Syntax: ftype fname(void){…} • Void function with void argument void fname(void){…}

  10. Advantages of Using Function Subprograms • Procedural Abstraction • The code for the sub problems (such as drawing a circle, or drawing a square, or drawing a triangle) is moved from the main function to function subprograms (i.e., function definitions) • We can (and should) defer implementation details until we are ready to write an individual function subprogram. • Reuse of Function Subprograms. • Functions can be called (executed) more than once in a program. • Functions written for a given program can be used in other programs.

  11. Tips for Using Functions • Don’t try to return a value that has a different type than the function’s type • Do use local variables whenever possible • Don’t let function get too long • Do limit each function to a single task • Don’t have multiple return statements if they are unnecessary • Do pass parameters to functions to make the function generic and thus reusable • Do take advantage of the ability to put functions into expression. • Don’t make an individual statement confusing by including too many functions • Do limit the use of nested function calls. f1(f2(a))

  12. On Scope of Variables • Scope of variables (identifiers) – determines where you can reference a variable (an identifier). • For variables (identifiers) declared before main function – scope is global – they can be referenced anywhere. • For all other identifiers, their scope is local - the function in which they are declared. See example

  13. Testing Functions • A function is an independent program module. It can be tested separately by a program which defines the argument, call the function and displays the result of the function call • Example ( see live example ) #include <stdio.h> double square(double); /* function prototype */ int main() /* this is a test driver for the function square() */ { double u, v; u = square(v); printf(“The square of %f is %d \n”, v, u); } double square(double y) { double b; b = y*y; return b; }

  14. Testing Function scale

  15. Data Areas After Call scale(num_1, num_2);

More Related