<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 15 Function Overloading_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 15: Function Overloading
 More of this Feature
• Function Overloading, Page 1
• Function Overloading, Page 2
• Function Overloading, Page 3
• Solution to Practice Problem
 
 Related Resources
• C++ Tutorial - Introduction to Pointers
• C++ Tutorial - References
• C++ Tutorial - Arrays and Vectors
• C++ Tutorial - Functions, Basics
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• All C++ Lessons
 

by John Kopp

Welcome to About.com's free tutorial on C++ programming. This lesson covers overloaded functions. Function overloading provides a way to have multiple functions with the same name. Why would this be of benefit? Let's look at a simple example. Suppose it's needed to have a function that converts a temperature from Fahrenheit to Celsius.

Prototype (Declaration):

void ConvertFToC(float f, float &c);

Definition:

void ConvertFToC(float f, float &c)
{
    c = (f - 32.) * 5./9.;
}

Now suppose that a Convert function is also needed to convert an integer Fahrenheit temperature to Celsius in the same program. One approach would be to give this function a new name, and perhaps to update the name of the "float" version as well.

void fConvertFToC(float f, float &c);
void iConvertFToC(int f, int &c);

That's not too bad. There are only two function names to keep track off. Now suppose that a higher precision, double, version is needed, or a version for data type short or long.

void fConvertFToC(float f, float &c);
void iConvertFToC(int f, int &c);
void dConvertFToC(double f, double &c);
void lConvertFToC(long f, long &c);
void sConvertFToC(short f, short &c);

As can be seen, this is quickly becoming a lexical nightmare. It is now necessary to remember five different function names for functions that essentially perform the same calculation. Someone reading or maintaining a program using these functions is bound to become quickly confused. It is not clear that exactly how exactly how each differs. Are only the data types different or are there differences in calculation? Fortunately, function overloading provides an alternative. The same function name can be used for multiple functions provided each differs in the number or type of its parameters. For each call of the function, the compiler compares the number and type of the arguments in the call against the parameter lists of each version of the function. The compiler selects the appropriate version. This process is call function resolution.

Next page > Function Overloading, Continued. > Page 1, 2, 3


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.