Chapter 5 Fuctios for All Subtasks Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Overview 5.1 void Fuctios 5.2 Call-By-Referece Parameters 5.3 Usig Procedural Abstractio 5.4 Testig ad Debuggig 5.5 Geeral Debuggig Techiques Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-3
5.1 void Fuctios Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
void-fuctios I top-dow desig, a subtask might produce No value (just iput or output for example) Oe value More tha oe value We have see how to implemet fuctios that retur oe value A void-fuctio implemets a subtask that returs o value or more tha oe value Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-5
void-fuctio Defiitio Two mai differeces betwee void-fuctio defiitios ad the defiitios of fuctios that retur oe value Keyword void replaces the type of the value retured void meas that o value is retured by the fuctio The retur statemet does ot iclude ad expressio Example: void show_results(double f_degrees, double c_degrees) { usig amespace std; cout << f_degrees << degrees Fahreheit is euivalet to << edl << c_degrees << degrees Celsius. << edl; retur; } Display 5.1 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-6
Usig a void-fuctio void-fuctio calls are executable statemets They do ot eed to be part of aother statemet They ed with a semi-colo Example: show_results(32.5, 0.3); NOT: cout << show_results(32.5, 0.3); Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-7
void-fuctio Calls Mechaism is early the same as the fuctio calls we have see Argumet values are substituted for the formal parameters It is fairly commo to have o parameters i void-fuctios I this case there will be o argumets i the fuctio call Statemets i fuctio body are executed Optioal retur statemet eds the fuctio Retur statemet does ot iclude a value to retur Retur statemet is implicit if it is ot icluded Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-8
Example: Covertig Temperatures The fuctios just developed ca be used i a program to covert Fahreheit temperatures to Celcius usig the formula C = (5/9) (F 32) Do you see the iteger divisio problem? Display 5.2 (1) Display 5.2 (2) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-9
void-fuctios Why Use a Retur? Is a retur-statemet ever eeded i a void-fuctio sice o value is retured? Yes! What if a brach of a if-else statemet requires that the fuctio eds to avoid producig more output, or creatig a mathematical error? void-fuctio i Display 5.3, avoids divisio by zero with a retur statemet Display 5.3 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-10
The Mai Fuctio The mai fuctio i a program is used like a void fuctio do you have to ed the program with a retur-statemet? Because the mai fuctio is defied to retur a value of type it, the retur is eeded C++ stadard says the retur 0 ca be omitted, but may compilers still require it Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-11
Sectio 5.1 Coclusio Ca you Describe the differeces betwee voidfuctios ad fuctios that retur oe value? Tell what happes if you forget the returstatemeti a void-fuctio? Distiguish betwee fuctios that are used as expressios ad those used as statemets? Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-12
5.2 Call-By-Referece Parameters Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Call-by-Referece Parameters Call-by-value is ot adequate whe we eed a sub-task to obtai iput values Call-by-value meas that the formal parameters receive the values of the argumets To obtai iput values, we eed to chage the variables that are argumets to the fuctio Recall that we have chaged the values of formal parameters i a fuctio body, but we have ot chaged the argumets foud i the fuctio call Call-by-referece parameters allow us to chage the variable used i the fuctio call Argumets for call-by-referece parameters must be variables, ot umbers Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-14
Call-by-Referece Example void get_iput(double& f_variable) { usig amespace std; cout << Covert a Fahreheit temperature << to Celsius.\ << Eter a temperature i Fahreheit: ; ci >> f_variable; } & symbol (ampersad) idetifies f_variable as a call-by-referece parameter Used i both declaratio ad defiitio! Display 5.4 (1) Display 5.4 (2) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-15
Call-By-Referece Details Call-by-referece works almost as if the argumet variable is substituted for the formal parameter, ot the argumet s value I reality, the memory locatio of the argumet variable is give to the formal parameter Whatever is doe to a formal parameter i the fuctio body, is actually doe to the value at the memory locatio of the argumet variable Display 5.5 (1) Display 5.5 (2) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-16
Call Comparisos Call By Referece vs Value Call-by-referece The fuctio call: f(age); Memory Name Locatio Cotets Call-by-value The fuctio call: f(age); age 1001 34 iitial 1002 A hours 1003 23.5 1004 void f(it& ref_par); void f(it var_par); Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-17
Example: swap_values void swap(it& variable1, it& variable2) { it temp = variable1; variable1 = variable2; variable2 = temp; } If called with swap(first_um, secod_um); first_um is substituted for variable1 i the parameter list secod_um is substituted for variable2 i the parameter list temp is assiged the value of variable1 (first_um) sice the ext lie will loose the value i first_um variable1 (first_um) is assiged the value i variable2 (secod_um) variable2 (secod_um) is assiged the origial value of variable1 (first_um) which was stored i temp Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-18
Mixed Parameter Lists Call-by-value ad call-by-referece parameters ca be mixed i the same fuctio Example: void good_stuff(it& par1, it par2, double& par3); par1 ad par3 are call-by-referece formal parameters Chages i par1 ad par3 chage the argumet variable par2 is a call-by-value formal parameter Chages i par2 do ot chage the argumet variable Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-19
Choosig Parameter Types How do you decide whether a call-by-referece or call-by-value formal parameter is eeded? Does the fuctio eed to chage the value of the variable used as a argumet? Yes? Use a call-by-referece formal parameter No? Use a call-by-value formal parameter Display 5.6 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-20
Iadvertet Local Variables If a fuctio is to chage the value of a variable the correspodig formal parameter must be a call-by-referece parameter with a ampersad (&) attached Forgettig the ampersad (&) creates a call-by-value parameter The value of the variable will ot be chaged The formal parameter is a local variable that has o effect outside the fuctio Hard error to fid it looks right! Display 5.7 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-21
Sectio 5.2 Coclusio Ca you Write a void-fuctio defiitio for a fuctio called zero_both that has two referece parameters, both of which are variables of type it, ad sets the values of both variables to 0. Write a fuctio that returs a value ad has a call-by-referece parameter? Write a fuctio with both call-by-value ad call-by-referece parameters Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-22
5.3 Usig Procedural Abstractio Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Usig Procedural Abstractio Fuctios should be desiged so they ca be used as black boxes To use a fuctio, the declaratio ad commet should be sufficiet Programmer should ot eed to kow the details of the fuctio to use it Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-24
Fuctios Callig Fuctios A fuctio body may cotai a call to aother fuctio The called fuctio declaratio must still appear before it is called Fuctios caot be defied i the body of aother fuctio Example: void order(it& 1, it& 2) { if (1 > 2) swap_values(1, 2); } swap_values called if 1 ad 2 are ot i ascedig order After the call to order, 1 ad 2 are i ascedig order Display 5.8 (1) Display 5.8 (2) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-25
Pre ad Postcoditios Precoditio States what is assumed to be true whe the fuctio is called Fuctio should ot be used uless the precoditio holds Postcoditio Describes the effect of the fuctio call Tells what will be true after the fuctio is executed (whe the precoditio holds) If the fuctio returs a value, that value is described Chages to call-by-referece parameters are described Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-26
swap_values revisited Usig precoditios ad postcoditios the declaratio of swap_values becomes: void swap_values(it& 1, it& 2); //Precoditio: variable1 ad variable 2 have // bee give values // Postcoditio: The values of variable1 ad // variable2 have bee // iterchaged Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-27
Fuctio celsius Precoditios ad postcoditios make the declaratio for celsius: double celsius(double fareheit); //Precoditio: fahreheit is a temperature // expressed i degrees Fahreheit //Postcoditio: Returs the equivalet temperature // expressed i degrees Celsius Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-28
Why use precoditios ad postcoditios? Precoditios ad postcoditios should be the first step i desigig a fuctio specify what a fuctio should do Always specify what a fuctio should do before desigig how the fuctio will do it Miimize desig errors Miimize time wasted writig code that does t match the task at had Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-29
Case Study Supermarket Pricig Problem defiitio Determie retail price of a item give suitable iput 5% markup if item should sell i a week 10% markup if item expected to take more tha a week Iput 5% for up to 7 days, chages to 10% at 8 days The wholesale price ad the estimate of days util item sells Output The retail price of the item Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-30
Supermarket Pricig: Problem Aalysis Three mai subtasks Iput the data Compute the retail price of the item Output the results Each task ca be implemeted with a fuctio Notice the use of call-by-value ad call-by-referece parameters i the followig fuctio declaratios Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-31
Supermarket Pricig: Fuctio get_iput void get_iput(double& cost, it& turover); //Precoditio: User is ready to eter values // correctly. //Postcoditio: The value of cost has bee set to // the wholesale cost of oe item. // The value of turover has bee // set to the expected umber of // days util the item is sold. Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-32
Supermarket Pricig: Fuctio price double price(double cost, it turover); //Precoditio: cost is the wholesale cost of oe // item. turover is the expected // umber of days util the item is // sold. //Postcoditio: returs the retail price of the item Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-33
Supermarket Pricig: Fuctio give_output void give_output(double cost, it turover, double price); //Precoditio: cost is the wholesale cost of oe item; // turover is the expected time util sale // of the item; price is the retail price of // the item. //Postcoditio: The values of cost, turover, ad price // bee writte to the scree. Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-34
Supermarket Pricig: The mai fuctio With the fuctios declared, we ca write the mai fuctio: it mai() { double wholesale_cost, retail_price; it shelf_time; get_iput(wholesale_cost, shelf_time); retail_price = price(wholesale_cost, shelf_time); give_output(wholesale_cost, shelf_time, retail_price); retur 0; } Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-35
Supermarket Pricig: Algorithm Desig -- price Implemetatios of get_iput ad give_output are straightforward, so we cocetrate o the price fuctio pseudocode for the price fuctio If turover <= 7 days the retur (cost + 5% of cost); else retur (cost + 10% of cost); Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-36
Supermarket Pricig: Costats for The price Fuctio The umeric values i the pseudocode will be represeted by costats Cost double LOW_MARKUP = 0.05; // 5% Cost double HIGH_MARKUP = 0.10; // 10% Cost it THRESHOLD = 7; // At 8 days use //HIGH_MARKUP Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-37
Supermarket Pricig: Codig The price Fuctio The body of the price fuctio { if (turover <= THRESHOLD) retur ( cost + (LOW_MARKUP * cost) ) ; else retur ( cost + ( HIGH_MARKUP * cost) ) ; } Display 5.9 (1) See the complete program i Display 5.9 (2) Display 5.9 (3) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-38
Supermarket Pricig : Program Testig Testig strategies Use data that tests both the high ad low markup cases Test boudary coditios, where the program is expected to chage behavior or make a choice I fuctio price, 7 days is a boudary coditio Test for exactly 7 days as well as oe day more ad oe day less Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-39
Sectio 5.3 Coclusio Ca you Defie a fuctio i the body of aother fuctio? Call oe fuctio from the body of aother fuctio? Give precoditios ad postcoditios for the predefied fuctio sqrt? Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-40
5.4 Testig ad Debuggig Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Testig ad Debuggig Fuctios Each fuctio should be tested as a separate uit Testig idividual fuctios facilitates fidig mistakes Driver programs allow testig of idividual fuctios Oce a fuctio is tested, it ca be used i the driver program to test other fuctios Fuctio get_iput is tested i the driver program of ad Display 5.10 (1) Display 5.10 (2) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-42
Stubs Whe a fuctio beig tested calls other fuctios that are ot yet tested, use a stub A stub is a simplified versio of a fuctio Stubs are usually provide values for testig rather tha perform the iteded calculatio Stubs should be so simple that you have cofidece they will perform correctly Fuctio price is used as a stub to test the rest of the supermarket pricig program i ad Display 5.11 (1) Display 5.11 (2) Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-43
Rule for Testig Fuctios Fudametal Rule for Testig Fuctios Test every fuctio i a program i which every other fuctio i that program has already bee fully tested ad debugged. Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-44
Sectio 5.4 Coclusio Ca you Describe the fudametal rule for testig fuctios? Describe a driver program? Write a driver program to test a fuctio? Describe ad use a stub? Write a stub? Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-45
5.5 Geeral Debuggig Techiques Copyright 2015 Pearso Educatio, Ltd.. All rights reserved.
Geeral Debuggig Techiques Stubs, drivers, test cases as described i the previous sectio Keep a ope mid Do t assume the bug is i a particular locatio Do t radomly chage code without uderstadig what you are doig util the program works This strategy may work for the first few small programs you write but is doomed to failure for ay programs of moderate complexity Show the program to someoe else Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-47
Geeral Debuggig Techiques Check for commo errors, e.g. Local vs. Referece Parameter = istead of == Localize the error This temperature coversio program has a bug Display 5.12 Narrow dow bug usig cout statemets Display 5.13 Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 1-48
Geeral Debuggig Techiques Use a debugger Tool typically itegrated with a developmet eviromet that allows you to stop ad step through a program lie-by-lie while ispectig variables The assert macro Ca be used to test pre or post coditios #iclude <cassert> assert(boolea expressio) If the boolea is false the the program will abort Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-49
Assert Example Deomiator should ot be zero i Newto s Method Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-50
Sectio 5.5 Coclusio Ca you Recogize commo errors? Use the assert macro? Debug a program usig cout statemets to localize the error? Debug a program usig a debugger? Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-51
Chapter 5 -- Ed Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-52
Display 5.1 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-53
Display 5.2 (1/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-54
Display 5.2 (2/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-55
Display 5.3 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-56
Display 5.4 (1/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-57
Display 5.4 (2/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-58
Display 5.5 (1/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-59
Display 5.5 (2/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-60
Display 5.6 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-61
Display 5.7 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-62
Display 5.8 (1/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-63
Display 5.8 (2/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-64
Display 5.9 (1/3) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-65
Display 5.9 (2/3) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-66
Display 5.9 (3/3) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-67
Display 5.10 (1/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-68
Display 5.10 (2/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-69
Display 5.11 (1/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-70
Display 5.11 (2/2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-71
Display 5.12 Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-72
Display 5.13 (1 of 2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-73
Display 5.13 (2 of 2) Back Next Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Slide 5-74