Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objectives Function Templates Recursion Functions
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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!
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Template Prefix template In this usage, "class" means "type", or "classification"
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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!
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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"!
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Function Template Example Swap values (even works for structures) Sorting an array int array double array
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Topic 2: Recursion Function A function that "calls itself" In function definition, call to same function
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Pitfall: Infinite Recursion Base case MUST eventually be entered If it doesn’t infinite recursion Recursive calls never end!
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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); }
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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); }
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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); }
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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"; }
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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); }