hey everyone I hope you are safe and doing good so in the series of learning C++ programming language we're discussing functions in C++ in previous video we have seen what are default arguments right how to use how to pass default arguments uh in functions so the next topic is function overloading this is also uh an important feature of functions right basically we have uh four oops uh concept or four oops pillars we can say abstraction classes and uh objects encapsulation and polymorphism that we'll see in detail in later videos right so this overloading is a part of polymorphism right we'll discuss in brief what are function overloading how to use how to achieve function overloading with help of program right but polymorphism we will see in later videos when we're discussing uh oops concept right so now see in this video we'll see what are basically why function overloading need of function overloading this thing is important if you studying any concept then what is need of that concept that feature right that is important so this thing we'll see in this video right now see you know what are function how to declare function how to define how to call function Basics are clear to you guys right see if I write something like this so now you can differentiate like this is function prototype or function declaration this is function calling and this is function definition right now if in your program you have two or more function with same name like in my program I have display one more function I have display right name is same but parameters you are passing those are different maybe the data type of parameter is different or maybe number of parameters are different right that thing is known as function overloading right see let me show you if in my program I have one more function display and here I have like string so in my program I have two functions with same name display display but parameters are different here I'm passing int here I'm passing string so obviously definition will be different here void display string Str Str and they see out I'll uh display St Str right and whenever you call then display maybe you'll pass next gen so while calling this I'll pass J A String this is a c style string this is also fine this is uh accepting uh expecting a C++ string like the string object but you are passing C style string a string lateral that is also fine it will be converted to C++ style string or a string object also you can pass right and same the definition is also some something different from this like string variable name is Str Str and I'm just printing here C out St Str right so in one program in the same scope or in one program I have two functions with same name but parameters are different so this is function overloading so why you need function overloading see sometimes if you want to display or if you want to print integer value suppose two I also want to print Jenny then I want to print like maybe 10.2 or double value or a character value also sometimes right so rather than taking different different function name like for printing integer I will take maybe print int and here the function the argument then print double then print string print character so you have to take four different names right and you have to remember like these four different names and all so rather than this using function overloading we can take only single name that is print print only right and if you want to print integer you just pass your int if you want to print string you just pass your string if you want to print double just print uh pass double as an argument and that's it right so function name is same but we will Define here for these four things four different functions right but fun function name would be same only the different uh difference is in the type of the argument or the type of the parameter you take for this we will take int for this we will take string here for this we will take double here for this we will take here character right so this is like the program would be more readable rather than having four different name just take one simple same name so program would be more readable and maintainable and it will increase the reusability of the program as well right that is why we use function overloading so you don't have to rather than you don't have to remember the user you don't have to remember the exact name for the functions because name is same only the argument you pass that is different and the compiler will automatically call the best match I mean at compile time the compiler chooses which which function overloads to call based sh the type and the number of argument you pass right that is had of compiler that will choose right out of suppose here we have display so if I call this display two so the compiler chooses which function to call this int based on the type of parameter if this is the function calling so at this time compiler will choose which function to call this function based on the argument the type of argument you pass that is uh string so it will will call this not this right you don't have to remember the exact name maybe here you have to take like display int display string so rather than remembering different different name you just have to remember only one name same name display that is you can say advantage of using function overloading right now the function overloading we can achieve in three forms based on the number of parameter you passed based on the type of parameter you passed and based on the order of the parameters you passed right remember this if if you write something like this right if I like here in fun and void fun maybe you think this is function overloading because return type of the function is different no we cannot achieve function overloading based on the function return time this this will give er only you can achieve only you can differentiate the function only you can achieve the function overload based on the parameter list either the number of parameter or the type of parameter or the order of parameter sequence of parameter not with the return type of the function this you have to remember this is not function overloading this will give error right so now let me show you all these things how to achieve function overloading with the help of program so first we will see overloading using different type of parameters right okay now let's take two two function void suppose we have function display and one I'm taking same name display with the string data type right so if you're using string better to include that header string okay one is display okay here int int and string the name of the parameter is optional you can pass because this is just function declaration and after main suppose I'm defining this function display for INT so int a here in definition name of the variable is compulsory so in a I'm taking and just passing just you know printing here whatever the value right that's it and for second display I'll take string maybe St Str and here just display that whatever string you pass so while calling let's call this display I'll call this display and in second one I will just pass J okay so we are having here two function with same name but different differentials the type of parameter you pass in one I'm passing INT in second I'm passing string so whenever the control will go to main function the first is display three So based on the type of the parameter the compiler chooses which function to call this function because here the type is the argument you are passing is integer so in a this function would be called let's run this and I'll show you what output you will get here see printing int value let me just clear the previous output and printing integer value three printing string value gen right so this is how we can achieve overloading based on the different type of parameter now number of parameter you can achieve function overloading using different you know sorry the different number of parameters so let's take one more thing here suppose we are taking one more example here I'm taking function add and in one I'm passing three parameter and second I'm passing two two and three see the parameter type is same data type int int but number of parameter are different so this is also this is how also we can achieve function overloading right based on different number of parameters type is same doesn't matter numbers are different right so here I'll just pass two and three only two and for second I'll pass 2 comma 3A 4 okay now this is just Declaration of the function let's define these functions okay see now whenever you call this add 2 three so the compiler will call this one the number of parameter are two so this function would be called right when you you write this at 2 3 4 then at this time compiler chooses to call this function ABC with three parameters so let's run this and see what output you will get here five and N yeah this is also function overloading right plus you can acheve overloading with uh that different sequence of parameters so if I take here like int one more add and there I'm taking int and one is double this is also function overloading right so if I call here add suppose first I'm passing three then 10.4 this will not give any error sorry point it's 10.4 right and suppose one more definition void add let's run this and I'll show you what output you will get here see three output 13.4 as well right and if I take here void add first is double second is in let's comment out previous to so here int double and here number of parameter type of parameter both are same but sequence is different in first one I passing int and after that double but in second I'm passing first double then int so this is also function overloading right this will also work fine so let's just comment out these two function definition and just call add now first is 34 Point suppose 5 and second is 1 s right if you do not Define the second function I just Define one int a and this also this one only right not this so let's run this and I'll show you what output you will get see see 13.4 and 35 why so because why calling this it's okay we are calling like this function in a and double so a plus b it will do 13.4 in second case first was we are passing double value and here it is expecting an INT value so it's okay it will do automatically type conversion the truncation of its 0.5 so it will take only 34 34 would be assigned to this int and it double one would be assigned so it will give 35 right so if exact match doesn't f doesn't found the compiler is not able to find the exact match so it will find the best possible match type conversion would be done right according to that the best possible match would be find the best possible matches this one only because double would be truncated to int right but if I have here the exact match double and int let's define this second one also so this is the exact match so in this case it will give 35 uh like 35.5 35.5 so it is finding exact match so this function would be called right okay now order of sequence of this parameter can also be used for overloading so some rules are to achieve function overloading first one the function name should be same or I can say must be same function name second thing the the this uh the type of parameters must be different or number of parameters must be different or sequence of parameters must be different right otherwise it will give error or let me just show you one more example here see here I have void print in print see function name is same return type is different right so here void print I'm just printing high in int print I'm just returning a right I'm just calling print here and for this int because it is returning integer value so I will accept that value in a variable of type integer that is X print this and see out X right but it will give error see the red line cannot overload functions distinguished by return type alone right here only the difference is return type is different everything is same but this is how you cannot achieve function overloading right at least the parameter should be maybe the number of parameter should be different type of parameter should be different or the order of parameter should be different then you can achieve function overloading right so function return type is not you know the single factor with that you can achieve function overloading this you need to remember right now third thing we have discussed uh all the three ways to achieve function overring but here you can have some ambiguities while you know doing this while ACH function overloading now some causes ofun that ambiguities are maybe the type conversion maybe you are using function with default argument maybe you are using function with pass by reference three reason can be there for that ambiguity you get right so let me just tell you this thing with example first if you have because of type conversion you can get function ambiguity let me just show you this thing with example see here I have one example there I have function print in this I'm passing INT in void print I'm passing float in print I'm passing string three print I have right and this is the definition for integer I'm just printing a for string the string value whatever you pass for float whatever float value you pass that would be printed so while calling I'm just passing first print 10 so ideally according to us it should call like 10 is integer value so this in so it will print print integer value a 10.2 this is float value so it should call this float so printing this printing double value D and it should print 10.2 right and third if I call like print and Jenny or rather than C style string if I take here one string object Str Str is equal to jeny and that object sorry not 8 also you can use but let's take a meaningful name s Str and here just pass print s Str an object string object C++ string object right so it should call this and it should print s strj but it will give an error to you guys let me show you that error okay this is not the exact error this is the error call of overloaded print double is ambiguous here 10. because in C++ all the floating Point number are treated as double right not float double by default right so there is type conversion from float to double so it is considering like 10.2 is not float it's double right so the exit match the compiler wants to call so but there is no double here int float string we are passing float here int here string here not double so there's no exact match found so compiler will call now the best possible match right so now this double can be treated as a float as well as truncated and treated as ink right so that is why this this error you are getting this call Print double in line number 15 is ambiguous right it can call the Sprint also it can call the Sprint float also so a compiler doesn't know which one to call either this or this so that is why it is giving ambiguous see while you're dragging your mouse to here then more than one instance of overload function print matches the argument list this also and this also con and floting but if you if you write here rather than float double so this is the exact match here rather than float double this will not give any error 10.2 is considered as double by default in C++ yeah we have this a print function with a double parameter so it will exactly match it is going to find and it will just print 10.2 integer value 10 10.2 string value J so while using function overloading you need to be very careful of these implicitly type conver type of thing right otherwise you will get error this is a drawback of using function overloading you have to be very careful second is if you're using function with default arguments let me show you that example also see one example we have here we have two function add and add in one I'm passing int int two parameter I'll pass two argument and in second I have int int int three parameter but one is default I'm taking one default if you will not pass anything then third argument it will take zero so here I'm passing default argument but in this case it will give error see while calling this add 2 three or let me just comment out these things two and three here you see red line more than one instance of overloaded function ad matches the argument list this one is also matches this one is also matches now which one to call function is the compiler doesn't know is not able to decide which one to call let me just run this and show you see the call of overloaded function add in in is ambiguous right because you are passing only two argument so this is matching with this also in ab and with this also because in this case also you are passing only two argument you will pass so by default the third argument it will take zero so third argument here it will take zero right so this is also ambiguity type of thing right so while using default arguments also you have to be very careful you can get this type of ambiguity error you got this right third thing if you use function with by uh with pass by reference in that case also you can get ambig error let me show you that thing also see here we have two function one is fun there will will pass int and another is fun they will passing like int and M per pass by reference we have discussed call by variable call by reference right call by reference we can achieve with by two ways like either using reference variable or using pointers here we are using reference variable right please watch out that video first if you are not aware about what is call by reference call call by value and what is reference variable so in in main so we have defined these two function fun there we passing just int a integer uh value I'm just printing a and here reference variable you are creating int h perent b and just we are calling B right and while calling we have int X is equal to 6 and we are just calling we're passing X here so like six we are passing so in this case also it is giving an ER like more than one instance of overloaded function fun matches the argument list this is also matching this is also matching so which function to call let me just run this and show you the output call of overloaded add in in okay let me just save this first and now run this see Call of overloaded fun in this m operator is ambiguous now compiler doesn't know which function to call because there is no syntactical difference between this fun in a and fun int m per B this is just reference variable reference variable means it's just an alas of the variable like whatever you will pass here x just another name right so this x this is also matching this is also matching in reference variable when you use then you simply pass here a variable not any address of variable or anything right so in this case also you are getting ambiguity error three reasons of of getting ambiguity error in while you are doing function overloading this thing you need to take care while you are doing function overloading in your program right so yeah we can say is this function overloading is a powerful feature in programming this will increase the readability of the code because you use only with only single function name you can achieve different different task you can do different different task based on the different parameters only right the function name is same so it increase the flexibility and reusability of your code right but obiously disadvantages are what you have to be more careful because there would be ambiguity error also so this thing you need to remember here right and compiler dependency also when you port your program from one compiler to another there maybe you can get some because of the some rules you get unpredictable results while you are you know achieving or doing function overloading in your program and debugging is also sometimes difficult because uh sometimes the error doesn't show exactly which function overload is causing that error so this is all about function overloading I hope you got this right so in the next video we'll see some coding exercise based on functions and function overloading right so now I'll see you in the next Tut bye take care