Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions (II) H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 24, 2011) Washington State University.

Similar presentations


Presentation on theme: "Functions (II) H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 24, 2011) Washington State University."— Presentation transcript:

1 Functions (II) H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 24, 2011) Washington State University

2 Case study : Advanced height converter Problem Statement: There is a need (as we have seen in the lab yesterday) to implement a program that prompts the user for height in feet and then converts the height to inches. Let’s say we assumed height should be read as a double number and user gives height in format feet.inch. Note that 1 foot is 12 inches. Inputs: ◦ heightInFeet Outputs ◦ inch Relevant formula: inch = inch_portion_of_height + feet_portion_of_height*12

3 3 Case study(2) Algorithm ◦ Get the height ◦ Calculate the inch equivalent of given feet-inch mixed format height  Calculate the count of decimal places in height (ignoring the leading zeros) **  Extract the whole number portion of height  Extract the precision portion of the height  Scale the precision to be a whole number, which gives the inch portion of the input  Calculate inch from feet and inch portion of height ◦ Display the result

4 Case study(3) We know how to perform all the steps except the one marked Let’s assume there is a user-defined library called decimalPlaceMaster.h and we have the chance to find its header(.h) and source file (.c).  Well, actually I implemented it, but that’s irrelevant We checked the source file, and realized it contains some instructions that we don’t know how to use for now. But, that’s OK, we don’t need to know how a function works to use it.

5 Case study(4) Let’s check how the prototype looks like which is located in the.h file. /* decimalPlaceCounter: Calculates the count of decimal places in a double(ignoring the leading zeros) Pre: - Post: Calculates the count of decimal places in a double(ignoring the leading zeros) up to 10 digits */ int decimalPlaceCounter (double num);

6 C. Hundhausen, A. O’Fallon6 A note on function comments Good style: Document the pre- and post-conditions of the function Preconditions: assumed to be true prior to executing the function Postconditions: assumed to be true after the function is done executing Style: /* read_int: reads an integer from file foo Pre: File foo is open for reading Next item to be read from foo is an integer Post: Return value contains value read in */

7 Case study(5) Seems like decimalPlaceCounter does exactly what we were looking for! Thanks to proper commenting we can easily see what this function does and what pre and post conditions of using the function are But we’re suspicious, we want to see if it works correctly. So, we decide to write a test-driver to test it. A test-driver is a short program that tests a specific function main function can act as a test-driver if we use it for the purpose of testing another function. Let’s write a separate function to act as the test-driver.

8 Case study(6) Let’s see how our our main.c file may look like calling the driver function: #include #include "decimalPlaceMaster.h" void testDriveFor_decimalPlaceCounter(void); int main(void) { testDriveFor_decimalPlaceCounter(); return 0; } void testDriveFor_decimalPlaceCounter(void) { //Test drive for the function decimalPlaceCounter printf("%d\n", decimalPlaceCounter(5)); printf("%d\n", decimalPlaceCounter(5.0)); printf("%d\n", decimalPlaceCounter(5.9)); printf("%d\n", decimalPlaceCounter(5.11)); printf("%d\n", decimalPlaceCounter(5.12345)); printf("%d\n", decimalPlaceCounter(5.1234567891)); printf("%d\n", decimalPlaceCounter(5.10001)); }

9 C. Hundhausen, A. O’Fallon9 Independent Function Testing Each function is itself a small-scale "program" ◦ It has inputs ◦ It has expected outputs or side-effects ◦ Ideally, it is a self-contained "black box" (does not manipulate global variables) It makes sense to test each function independently, so that its correctness can be verified before it is used in a larger scale application

10 Case study(7) After we tested the decimalPlaceCounter function, we can now safely use it in our program. We should always test functions we write properly. That’s one of the most important steps of ‘Software Development Method’.

11 A note on multiple file format and header guards #include #include “program.h”. int main(void) {. } #include “program.h” //function definitions #ifndef PROGRAM_H #define PROGRAM_H #include //constant macros //function prototypes. #endif Source Files/main.cSource Files/program.c Header Files/program.h Header guards (conditional define statement) protects one from getting redefinition errors This 3 format file will be a must-to-have for you in future programming assignments. Besides, it is good practice and as seen in the case study (decimalPlaceMaster library), one get to reuse or distribute his own code by complying to this practice.

12 C. Hundhausen, A. O’Fallon12 A note on local vs global variables Local versus global variables ◦ Global variables  Declared outside of a function (usually after the #define and #include statements) ◦ Local variables  Declared within a function  Only visible from within that function; once function is done, variables go away (space is deallocated)  Local variables are by default considered automatic variables; auto may be placed in front of these variables but is not required ◦ Notice: NO GLOBAL VARIABLES!  In general, they’re a bad idea  Why?

13 C. Hundhausen, A. O’Fallon13 Common Programming Errors Forgetting to include proper #include directives Forgetting to include user defined headers with header file name in double quotes (<> won’t work since the paths headers searched in when surrounded by <> does not include your project space) Not matching actual arguments to formal parameters (remember, number, order, and type matter) Calling a function with input data for which the function is undefined

14 Advanced height converter – alternative solution In the discussion in our 2 nd lab, we were consumed by the idea that we should read height as a double. Whereas, we didn’t have to, and here is an alternative solution to the same problem: #include #define FEET_IN_INCHES 12 int main(void) { int height_feet = 0, height_inch = 0, resultInch = 0; scanf(“%d.%d”, &height_feet, & height_inch); resultInch = height_feet + FEET_IN_INCHES* height_inch; printf(“Height in inches: %d\n”, resultInch); }

15 Advanced height converter – alternative solution (II) Take home messages: ◦ Sometimes looking at a problem from a different perspective helps to find a more concise (i.e. less complex) solution. ◦ Try to think of a number of ways to solve a problem and playing with the structure charts of your high- level algorithms, have a plan on your mind before start coding. ◦ Start discussions like the one in the lab with your friends, in class, in the labs. Ask “how can I do this” type of questions to yourself, to your friend, to me.


Download ppt "Functions (II) H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 24, 2011) Washington State University."

Similar presentations


Ads by Google