Featured Post

How Computers Understand Human Language?

How To Code? Function Overloading

function overloading in programming -img

If you are thinking how does this image relates to the topic, don't think. Keep calm and keep reading.

What is function overloading?

Function overloading is a concept in programming where there are multiple functions with the same name having different parameters. Different parameters mean either the number of parameter is different or the data type or the order of input parameters is different.
If the names of multiple functions are the same then how does a compiler/interpreter recognize which function has been called?
Recall function signature from the previous post. Compiler recognizes each function by its signature and function signature includes function name, number of parameters passed to it, their data type and the order in which they are provided. Each version of an overloaded function is almost same for us but entirely a different function for compiler. It just executes the function to which a call has been made irrespective of it is an overloaded function or not provided that the function call is a legal statement. When a compiler encounters a function call, it decides which function to execute from overloaded candidates on the basis of function signature.

Where to use function overloading?

Let me tell you now how the above image is relative to function overloading. Every screw wrench is used for the same purpose. They all do the same work but with a slight difference. Not every wrench fit for all screw sizes. A certain size of wrench can work only on that size of screw. You know, an overloaded function is like a wrench. For a set of alike problems, different versions of the same functions are more useful.
Whenever there is a group of tasks which are similar to each other but have a slight difference, function overloading come in handy. For example, you have some integer numbers and some floating point number and you are required to write a function which accepts a number and return its cube. Now you have a group of two similar tasks, calculating cube and a slight difference, different types of data. Function overloading in this scenario is considered the best practice.

Why to use function overloading?

Function overloading is considered one of the best practices of programming and programmers are encourage to use it because:
  1. it improves code readability and reduces the effort to remember function names
  2. it is used to implement polymorphism
  3. it makes code maintenance easy
Personally, I love the concept of function overloading when I work with a library. Believe me it saves a lot of time and effort.

Things to remember:

To overload a function:
  1. the name of the functions must be the same
  2. the number of parameters could be different
  3. types of parameters could be different 
  4. or both the number and types of parameters could be different

Next: How To Become A Successful Programmer? 5 Step Process

Comments