DOC PREVIEW
UD CISC 181 - Introduction to Computer Science

This preview shows page 1-2-3-22-23-24-44-45-46 out of 46 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CISC181 Introduction to Computer Science Dr. McCoy Lecture 8 September 24, 2009Constant Reference ParametersParameters and ArgumentsMixed Parameter ListsOverloadingOverloading Example: AverageOverloaded Average() Cont’dOverloading PitfallOverloading ResolutionOverloading Resolution ExampleAutomatic Type Conversion and Overloading ExampleDefault ArgumentsDefault Arguments Example: Display 4.1 Default Arguments (1 of 2)Default Arguments Example: Display 4.1 Default Arguments (2 of 2)Testing and Debugging FunctionsThe assert MacroAn assert Macro ExampleAn assert Macro Example Cont’dassert On/OffStubs and DriversDriver Program Example: Display 4.9 Driver Program (1 of 3)Driver Program Example: Display 4.9 Driver Program (2 of 3)Driver Program Example: Display 4.9 Driver Program (3 of 3)StubsFundamental Testing RuleRecursive FunctionsSlide 273.12 RecursionSlide 29fig03_14.cpp (1 of 2)fig03_14.cpp (2 of 2) fig03_14.cpp output (1 of 1)Recursive void Function: Vertical NumbersVertical Numbers: Recursive DefinitionwriteVertical Function DefinitionwriteVertical TraceRecursion—A Closer LookRecursion Big PictureInfinite RecursionA Recursive Power Function3.13 Example Using Recursion: Fibonacci SeriesSlide 41Slide 42fig03_15.cpp (1 of 2)fig03_15.cpp (2 of 2) fig03_15.cpp output (1 of 2)fig03_15.cpp output (2 of 2)3.14 Recursion vs. Iteration1CISC181 Introduction to Computer ScienceDr. McCoyLecture 8September 24, 2009Constant Reference Parameters•Reference arguments inherently"dangerous"–Caller’s data can be changed–Often this is desired, sometimes not•To "protect" data, & still pass by reference:–Use const keyword•void sendConstRef( const int &par1,const int &par2);•Makes arguments "read-only" by function•No changes allowed inside function body4-2Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Parameters and Arguments•Confusing terms, often used interchangeably•True meanings:–Formal parameters•In function declaration and function definition–Arguments•Used to "fill-in" a formal parameter•In function call (argument list)–Call-by-value & Call-by-reference•Simply the "mechanism" used in plug-in process4-3Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Mixed Parameter Lists•Can combine passing mechanisms•Parameter lists can include pass-by-valueand pass-by-reference parameters•Order of arguments in list is critical:void mixedCall(int & par1, int par2, double & par3);–Function call:mixedCall(arg1, arg2, arg3);•arg1 must be integer type, is passed by reference•arg2 must be integer type, is passed by value•arg3 must be double type, is passed by reference4-4Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Overloading•Same function name•Different parameter lists•Two separate function definitions•Function "signature"–Function name & parameter list–Must be "unique" for each function definition•Allows same task performed on different data4-5Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Overloading Example: Average•Function computes average of 2 numbers:double average(double n1, double n2){return ((n1 + n2) / 2.0);}•Now compute average of 3 numbers: KFM Changed slide…double average(double n1, double n2, double n3){return ((n1 + n2 + n3) / 3.0);}•Same name, two functions4-6Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Overloaded Average() Cont’d•Which function gets called?•Depends on function call itself:–avg = average(5.2, 6.7);•Calls "two-parameter average()"–avg = average(6.5, 8.5, 4.2);•Calls "three-parameter average()"•Compiler resolves invocation based onsignature of function call–"Matches" call with appropriate function–Each considered separate function4-7Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Overloading Pitfall•Only overload "same-task" functions–A mpg() function should always performsame task, in all overloads–Otherwise, unpredictable results•C++ function call resolution:–1st: looks for exact signature–2nd: looks for "compatible" signature4-8Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Overloading Resolution•1st: Exact Match–Looks for exact signature•Where no argument conversion required•2nd: Compatible Match–Looks for "compatible" signature whereautomatic type conversion is possible:•1st with promotion (e.g., intdouble)–No loss of data•2nd with demotion (e.g., doubleint)–Possible loss of data4-9Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Overloading Resolution Example•Given following functions:–1. void f(int n, double m);2. void f(double n, int m);3. void f(int n, int m);–These calls:f(98, 99);  Calls #3f(5.3, 4);  Calls #2f(4.3, 5.2);  Calls ???•Avoid such confusing overloading4-10Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Automatic Type Conversion and Overloading Example•double mpg(double miles, double gallons){return (miles/gallons);}•Example function calls:–mpgComputed = mpg(5, 20);•Converts 5 & 20 to doubles, then passes–mpgComputed = mpg(5.8, 20.2);•No conversion necessary–mpgComputed = mpg(5, 2.4);•Converts 5 to 5.0, then passes values to function4-11Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Default Arguments•Allows omitting some arguments •Specified in function declaration/prototype–void showVolume( int length,int width = 1,int height = 1);•Last 2 arguments are defaulted–Possible calls:•showVolume(2, 4, 6); //All arguments supplied•showVolume(3, 5); //height defaulted to 1•showVolume(7); //width & height defaulted to 14-12Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Default Arguments Example: Display 4.1 Default Arguments (1 of 2)4-13Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Default Arguments Example: Display 4.1 Default Arguments (2 of 2)4-14Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Testing and Debugging Functions•Many methods:–Lots of cout statements•In calls and definitions•Used to "trace" execution–Compiler Debugger•Environment-dependent–assert Macro•Early termination as needed–Stubs and drivers•Incremental development4-15Copyright © 2010 Pearson Addison-Wesley. All rights reserved.The assert Macro•Assertion: a true or false statement•Used to document and check correctness–Preconditions & Postconditions•Typical assert use:


View Full Document
Download Introduction to Computer Science
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Computer Science and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Computer Science 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?