Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-1 Today’s Learning Objectives  Function Templates  Recursion Functions.

Similar presentations


Presentation on theme: "Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-1 Today’s Learning Objectives  Function Templates  Recursion Functions."— Presentation transcript:

1 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-1 Today’s Learning Objectives  Function Templates  Recursion Functions

2 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-2 Topic 1  C++ templates  Allow very "general" definitions for functions  Parameter type names are "parameters" instead of actual types  Precise type is determined at run-time

3 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-3 Function Templates  Recall function swapValues: void swapValues(int & var1, int & var2) { int temp; temp = var1; var1 = var2; var2 = temp; }  Applies only to variables of type int  But algorithm would work for any types!

4 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-4 Function Templates vs. Overloading  Could overload function for char’s: void swapValues(char & var1, char & var2) { char temp; temp = var1; var1 = var2; var2 = temp; }  But notice: code is nearly identical!  Only difference is type used in 3 places

5 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-5 Function Template Syntax  Allow "swap values" of any type variables: template void swapValues(T & var1, T & var2) { T temp; temp = var1; var1 = var2; var2 = temp; }  First line called "template prefix"  Tells compiler what’s coming is "template"  And that T is a type parameter

6 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-6 Template Prefix  template  In this usage, "class" means "type", or "classification"

7 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-7 Template Prefix 2  template  T can be replaced by any type  Predefined or user-defined (like a C++ class type)  In function definition body:  T used like any other type  Note: can use other than "T", but T is "traditional" usage

8 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-8 Calling a Function Template  Consider following call: swapValues(int1, int2);  C++ compiler "generates" function definition for two int parameters using template  Likewise for all other types

9 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-9 Multiple Type Parameters  Can have: template  Not typical  Usually only need one "replaceable" type  Cannot have "unused” template parameters  Each must be "used" in definition  Error otherwise!

10 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-10 Inappropriate Types in Templates  Can use any type in template for which code makes "sense"  Code must behave in appropriate way  e.g., swapValues() template function  Cannot use type for which assignment operator isn’t defined  Example: an array: int a[10], b[10]; swapValues(a, b);  Arrays cannot be "assigned"!

11 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-11 Function Template Example  Swap values (even works for structures)  Sorting an array  int array  double array

12 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-12 Topic 2: Recursion Function  A function that "calls itself"  In function definition, call to same function

13 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-13 Pitfall: Infinite Recursion  Base case MUST eventually be entered  If it doesn’t  infinite recursion  Recursive calls never end!

14 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-14 Recursion Versus Iteration  Recursion not always "necessary"  Not even allowed in some languages  Any task accomplished with recursion can also be done without it  Nonrecursive: called iterative, using loops  Recursive:  Runs slower, uses more storage  Elegant solution; less coding

15 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-15 Recursion Examples  Calculate the summation of 1, 2, 3, …, n int sum(int n) { if (n==1) return 1; else return n + sum(n-1); }

16 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-16 Recursion Examples (continued)  Calculate the Factorial of a number n (n!=1*2*3*…*n) Int factorial(int n) { if (n==1) return 1; else return n * factorial(n-1); }

17 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-17 Recursion Examples (continued)  What results will be displayed by calling function message (5)? void message(int times) { if (times==0)return; else { cout << “This is an alert!\n”; message(times-1); }

18 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-18 Recursion Examples (continued)  What results will be displayed by calling function showMe (5)? void showMe(int n) { if(n==0)return; else { showMe(n-1); cout << n << "\t"; }

19 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-19 Recursion Examples (continued)  What results will be displayed by calling function showYou (5)? void showYou(int n) { if(n==0)return; else { cout << n << "\t"; showYou(n-1); }


Download ppt "Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-1 Today’s Learning Objectives  Function Templates  Recursion Functions."

Similar presentations


Ads by Google