more about math functions (preview version)

15
CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 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) =

Upload: gray-gould

Post on 01-Jan-2016

32 views

Category:

Documents


2 download

DESCRIPTION

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) = - PowerPoint PPT Presentation

TRANSCRIPT

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 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) =

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 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;

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 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);

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 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 declarationstatements}

• 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)

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 5

Function with Input Arguments and One Result

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 6

Functions find_circum and find_area

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 7

Effect of Executing circum = find_circum (radius);

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 8

Memory allocation with function and function call

main functionArea = find_area(radius)

Circum = find_circum(radius)

find_area( )

radiusarea

circum

rresult

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_circum( )

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 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){…}

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 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.

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 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))

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 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

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 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;

}

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 14

Testing Function scale

CP104 Introduction to Programming Top-down design with functions Lecture 8 __ 15

Data Areas After Call scale(num_1, num_2);