Skip to main content

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


Popular posts from this blog

Introduction to Data Science: What is Big Data?

What Is Big Data First, we will discuss how big data is evaluated step by step process. Evolution of Data How the data evolved and how the big data came. Nowadays the data have been evaluated from different sources like the evolution of technology, IoT(Internet of Things), Social media like Facebook, Instagram, Twitter, YouTube, many other sources the data has been created day by day. 1. Evolution of  Technology We will see how technology is evolved as we see from the below image at the earlier stages we have the landline phone but now we have smartphones of Android, IoS, and HongMeng Os (Huawei)  that are making our life smarter as well as our phone smarter. Apart from that, we have heavily built a desktop for processing of Mb's data that we were using a floppy you will remember how much data it can be stored after that hard disk has been introduced which can stored data in Tb. Now due to modern technology, we can be stored data in the cloud as well. Similarly, nowadays we noticed …

How Big Data Analytics Can Help You Improve And Grow Your Business?

Big Data Analytics There are certain problems that can only solve through big data. Here we discuss the field big data as "Big Data Analytics". The big data came into the picture we never thought how commodity hardware is used to store and manage the data which is reliable and feasible as compared to the costly sources. Now let us discuss a few examples of how big data analytics is useful nowadays. When you go to websites like Amazon, Youtube, Netflix, and any other websites actually they will provide some field in which recommend some product, videos, movies, and some songs for you. What do you think about how they do it? Basically what kind of data they generated on these kind websites. They make sure to analyze properly. The data generated is not small it is actually big data. Now they analysis these big data they make sure whatever you like and whatever you are the preferences accordingly they generate recommendations for you. If you go to Youtube you have noticed it kn…

AI Vs Machine Learning Vs Deep Learning

AI Vs Machine Learning Vs Deep Learning Artificial intelligence, deep learning and machine learning are often confused with each other. These terms are used interchangeably but do they do not refer to the same thing. These terms are closely related to each other which makes it difficult for beginners to spot differences among them. The reason I think of this puzzle is that AI is classified in many ways. It is divided into subfields with respect to the tasks AI is used for such as computer vision, natural language processing, forecasting and prediction, with respect to the type of approach used for learning and the type of data used. Subfields of Artificial Intelligence have much in common which makes it difficult for beginners to clearly differentiate among these areas. Different approaches of AI can process similar data to perform similar tasks. For example Deep learning and SVM both could be used for object detection task. Both have pros and cons. In some cases Machine Learning is …