lect6_2

lect6_2 - Function Templates 1 Function Templates Recall...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Function Templates Recall our discussion on function overloading. Consider the two overloaded versions of the swap function. // swaps int variables int temp = x; x = y; y = temp; } // end function swap ints // swaps double variables double temp = x; x = y; y = temp; } // end function swap doubles What if we have a need for interchanging two variables of some type, which is neither int nor double . For example, we want to swap two character strings, or some other user defined type, let us call this data type Type . Writing the functions for each of these individual types is not difficult because the statements are so similar. Each swap function definition looks like the following. // swaps two variables of type Type Type temp = x; x = y; y = temp; } // end function swap Types So, to write the function definition of a swap function for a particular data type, we would replace the word Type with the actual data type. Although writing each individual swap function is not difficult, it is needlessly repetitious. Basically, what is the task accomplished by a swap function. Given any two variables of identical data type, the function swaps their values. The procedure used in each of the functions has the same pattern or template.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/07/2008 for the course CS 181 taught by Professor Satya during the Fall '08 term at Stevens.

Page1 / 5

lect6_2 - Function Templates 1 Function Templates Recall...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online