BIL 104E Introduction to Scientific and Engineering Computing

BIL 104E Introduction to Scientific and Engineering Computing

BIL 104E Introduction to Scientific and Engineering Computing Lecture 4 Introduction • Divide and Conquer – Construct a program from smaller pieces or components • These smaller pieces are called modules • Functions – Modules in C – Programs combine user-defined functions with library functions • C standard library has a wide variety of functions 10/4/11 Lecture 4 2 Math Library Functions • Math library functions – perform common mathematical calculations – #include <math.h> • Format for calling functions – FunctionName( argument ); • If multiple arguments, use comma-separated list – printf( "%.2f", sqrt( 900.0 ) ); • Calls function sqrt, which returns the square root of its argument • All math functions return data type double – Arguments may be constants, variables, or expressions 10/4/11 Lecture 4 3 Math Library Functions Function Description Example sqrt( x ) square root of x sqrt( 900.0 ) is 30.0 sqrt( 9.0 ) is 3.0 exp( x ) exponential function ex exp( 1.0 ) is 2.718282 exp( 2.0 ) is 7.389056 log( x ) natural logarithm of x log( 2.718282 ) is 1.0 log( 7.389056 ) is 2.0 (base e) log10( x ) logarithm of x (base 10) log10( 1.0 ) is 0.0 log10( 10.0 ) is 1.0 log10( 100.0 ) is 2.0 fabs( x ) absolute value of x fabs( 5.0 ) is 5.0 fabs( 0.0 ) is 0.0 fabs( -5.0 ) is 5.0 ceil( x ) rounds x to the smallest ceil( 9.2 ) is 10.0 ceil( -9.8 ) is -9.0 integer not less than x floor( x ) rounds x to the largest floor( 9.2 ) is 9.0 floor( -9.8 ) is -10.0 integer not greater than x pow( x, y ) x raised to power y (xy) pow( 2, 7 ) is 128.0 pow( 9, .5 ) is 3.0 fmod( x, y ) remainder of x/y as a fmod( 13.657, 2.333 ) is 1.992 floating point number sin( x ) trigonometric sine of x sin( 0.0 ) is 0.0 (x in radians) cos( x ) trigonometric cosine of x cos( 0.0 ) is 1.0 (x in radians) tan( x ) trigonometric tangent of x tan( 0.0 ) is 0.0 (x in radians) Fig. 5.2 Commonly used math library functions. 10/4/11 Lecture 4 4 User-Defined Functions • Functions (or modules) are the sets of statements that typically perform an operation or that compute a value. • To maintain simplicity and readability in longer and more complex problem solutions, instead of using one long main function, we develop programs that use a main function plus additional functions. • The execution of a program always begins with the main function. Additional functions are called, or invoked, when the program encounters function names. These additional functions must be defined in the file containing the main function or in another file or library of files. After executing the statements in a function, the program execution continues with the statement that called the function. 10/4/11 Lecture 4 5 Advantages of Modular Programming • A module can be written and tested separately from other parts of the solution; thus, module development can be done in parallel for large projects. • A module is a small part of the solution; thus, testing it separately is easier. • Once a module is tested carefully, it does not need to be retested before it can be used in new problem solutions.(reusability) • The use of modules usually reduces the length of a program, making it more readable. • The use of modules promotes the concept of abstraction, which allows the programmer to “hide” the details in modules. This allows us to use modules in a functional sense without being concerned about the specific details. 10/4/11 Lecture 4 6 Function Definitions • Functions can be defined before or after the main function. However, one function must be completely defined before another function begins; function definitions cannot be nested within each other. • A function consists of a definition statement followed by declarations and statements. If the function does not return a value, the type is void. Thus the general form of a function is: return_type function_name (parameter declarations) { declarations; statements; } • The parameter declarations represent the information passed to the function. If there are no input parameters (also called arguments), then the parameter declarations should be void. Additional variables used by a function are defined in the declarations. 10/4/11 Lecture 4 7 Function Definitions • All functions should include a return statement, which has the following general form: return expression; • The expression specifies the value to be returned to the statement that referenced the function. The expression type should match the return type indicated in the function definition to avoid potential errors. • A void function does not return a value and thus has this general statement: void function_name (parameter declarations) • The return statement in a void function does not contain an expression and has the form: return; 10/4/11 Lecture 4 8 Function Prototype • Function prototype statements should be included for all functions referenced in a program. The general form of a function prototype is: return_type function_name (parameter_data_types); • A function prototype can be included with preprocessor directives, or because a function prototype is defining the type of value being returned by the function, it can also be included with other variable declarations. • If a programmer-defined function references other programmer- defined functions, it will also need additional prototype statements. 10/4/11 Lecture 4 9 Function Prototype • Header files, such as stdio.h and math.h, contain the prototype statements for many of the functions in the Standard C library. • If a program references a large number of programmer-defined functions, it becomes cumbersome to include all the function prototype statements. In these cases, a custom header file can be defined that contains the function prototypes and related symbolic constants. • A header file must have a filename that ends with a suffix of .h. The file is then referenced with an include statement using double quotes around the file name. Custom header files are often used to accompany routines that are shared by programmers. 10/4/11 Lecture 4 10 Parameter List • The definition statement of a function defines parameters that are required by the function; these are called formal parameters. • Any statement that references the function must include values that correspond to the parameters; these are called actual parameters. • When the function is referenced the value in the ‘actual parameters’ are copied to the ‘formal parameters’ and the steps in the function are executed using the new values in the ‘formal parameters’. • If a function has more than one parameter, the formal parameters and the actual parameters must match in number, type and order. • Valid references to the function can also include expressions and can include other function references 10/4/11 Lecture 4 11 Example • Assume that the definition statement of the function f is double f (double x) { … } • The following references to function f are valid: printf(“%f \n”, f(x+2.5)); printf(“%f \n”, f(y)); z = x*x + f(2*x) ; w = f(fabs(y)) ; • Here, the formal parameter is still x, but the actual parameter changes depending on the reference selected. 10/4/11 Lecture 4 12 1 /* Fig. 5.3: fig05_03.c 2 Creating and using a programmer-defined function */ 3 #include <stdio.h> 4 5 int square( int y ); /* function prototype */ 6 7 /* function main begins program execution */ 8 int main() 9 { 10 int x; /* counter */ 11 12 /* loop 10 times and calculate and output square of x each time */ 13 for ( x = 1; x <= 10; x++ ) { 14 printf( "%d ", square( x ) ); /* function call */ 15 } /* end for */ 16 17 printf( "\n" ); 18 19 return 0; /* indicates successful termination */ 20 21 } /* end main */ 22 23 /* square function definition returns square of an integer */ 24 int square( int y ) /* y is a copy of argument to function */ 25 { 26 return y * y; /* returns square of y as an int */ 27 28 } /* end function square */ 1 4 9 16 25 36 49 64 81 100 10/4/11 Lecture 4 13 1 /* Fig. 5.4: fig05_04.c 2 Finding the maximum of three integers */ 3 #include <stdio.h> 4 5 int maximum( int x, int y, int z ); /* function prototype */ 6 7 /* function main begins program execution */ 8 int main() 9 { 10 int number1; /* first integer */ 11 int number2; /* second integer */ 12 int number3; /* third integer */ 13 14 printf( "Enter three integers: " ); 15 scanf( "%d%d%d", &number1, &number2, &number3 ); 16 17 /* number1, number2 and number3 are arguments 18 to the maximum function call */ 19 printf( "Maximum is: %d\n", maximum( number1, number2, number3 ) ); 20 21 return 0; /* indicates successful termination */ 22 23 } /* end main */ 24 10/4/11 Lecture 4 14 25 /* Function maximum definition */ 26 /* x, y and z are parameters */ 27 int maximum( int x, int y, int z ) 28 { 29 int max = x; /* assume x is largest */ 30 31 if ( y > max ) { /* if y is larger than max, assign y to max */ 32 max = y; 33 } /* end if */ 34 35 if ( z > max ) { /* if z is larger than max, assign z to max */ 36 max = z; 37 } /* end if */ 38 39 return max; /* max is largest value */ 40 41 } /* end function maximum */ Enter three integers: 22 85 17 Maximum is: 85 Enter three integers: 85 22 17 Maximum is: 85 Enter three integers: 22 17 85 Maximum is: 85 10/4/11 Lecture 4 15 Header Files • Header files – Contain function prototypes for library functions – <stdlib.h> , <math.h> , etc – Load with #include <filename> #include <math.h> • Custom header files – Create file with functions – Save as filename.h – Load in other files with #include "filename.h" – Reuse functions 10/4/11 Lecture 4 16 Header Files Standard library header Explanation <assert.h> Contains macros and information for adding diagnostics that aid program debugging.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    28 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us