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

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 16 Templates. Learning Objectives Function Templates – Syntax, defining – Compiler complications Class Templates – Syntax – Example: Pair class.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Recursion. Binary search example postponed to end of lecture.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
ISBN Chapter 10 Implementing Subprograms.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Recursion.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Csci1300 Introduction to Programming Recursion Dan Feng.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Stacks and Queues. Sample PMT online… Browse 1120/sumII05/PMT/2004_1/
Copyright © 2003 Pearson Education, Inc. Slide 1.
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
ISBN Chapter 10 Implementing Subprograms.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Chapter 13 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Recursive Functions for Tasks(13.1) Recursive Functions.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 14 Recursion.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Copyright 2006 Pearson Addison-Wesley, 2008, 2009 Joey Paquet 9-1 Concordia University Department of Computer Science and Software Engineering COMP345.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
A First Book of ANSI C Fourth Edition
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
ISBN Chapter 10 Implementing Subprograms.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
User-Written Functions
Recursion DRILL: Please take out your notes on Recursion
Java 4/4/2017 Recursion.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
CSC 143 Recursion.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

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); }