Function with default arguments in C++ | C ++ Tutorial | Mr. Kishore

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi welcome to nourish City this is Kishore and today we are going to discuss about functions with the default argument okay today our topic is functions with the default arguments actually it is a new concept introduced in C++ now what is called functions with default arguments first of all I am doing small example for easy understanding of functions with the default argument okay now watch this example now I am giving a small C example first of all okay Colin had help now I am going to write your function one function for example int sum that means I want to perform the sum of three numbers or two numbers what it may be no it is a function declaration and here I am saying like this int X next int yd and int yet okay now it is the function declaration part now when function declaration is there it should be ended with semicolon because of prototyping okay now it indicates what this kind of function we are going to use in future okay fine later I am going for main function also now in main function we have to call the function means in main function I am going to call the sum function and here I said I am going to pass 3 arguments to this function okay here the point is I have to send three arguments for this function that's why CL naught is here first now the screen content player later Here I am going to write like for example I am going to write the seed program in C++ okay now see out some equal to some off some values are equal now 10-20-30 unladen and l okay here what happened watch it now Co Tod is a printing now some equal to the text it is lending as it is on the screen later some of ten twenty thirty okay fight like another function goal see out some equal to sum of ten twenty candle next another function call some of only tell candle next another function calls some equal to sum of nothing now bracket close okay and here we have or I have called the same function for time okay here in this example I have called the sum function for thanks and what happened here in first function call I am going to send three organized in second function call only two arguments in third one only one unlawful nothing is there okay now we need the definition for this function okay for example I am going to give the definition here int sum of int X comma int y comma int Jade next simply I am going to say a return X plus y plus Z okay now it is the function definition okay generally C or C++ program execution starts from main function everybody knows now here also programming tribution starts from this point later CLRS here green content eraser later see out from equal to it is the first function calling in first function calling how many arguments were how use it a three now this 10 positive X I'm 20 partial 2y and 30 positive yet okay now XY jet are receiving the values from 10 20 30 that means ten pursuit to X 20 password to Y 30 posited that is why X becomes 10 y becomes 20 just become static now return x + y + y that means what 10 plus 20 plus 30 now the total 60 it is going to return now that return value is going to print here that is why your answer is 6 feet under in general ok now come to the second function call here how many arguments we are sending means only two or give it 10 20 now the 10 is received by x + 20 is received by Y because of arguments are always positive from left to right that means the formal parameters are replaced from left to right okay here arguments are sending from right to left but they are received from left to right that's a what happens 10 goes to X 20 goes to now what about the Jade okay here JD is not having any corresponding value actually in function declaration what I said I will send 3 arguments for this function actually function declaration is also called function prototyping and the rule is function prototyping should be matched with function definition here function prototype definition both are same there is no problem at all but the thing is function calling is not naturally to the prototype or definition why because here prototype is having CLE argument here also pre argument but here only two arguments now it is going to give error in C compiler okay now this program is going to return here here and the same thing is happened here also because of only one argument you are submitting but requirement is three and here there is no argument that's why this program gives three error okay then to solve this problem to follow this problem in sleeplessness they have introduced the concept of functions with default argument okay now in function with default arguments what happening actually it is function declaration or prototype now now we can declare the functions with default values that means here we can assign the value here also we can assign the value here also we can say in the way now what happens when corresponding argument is missing in function call they are replaced with the declaring values it is the functions with default argument concept that's like in functions with default arguments we are going to declare the formal parameters with initial values for example okay here I am going to declare X equal to 10 suppose y equal to 20 or X equal to 1 y equal to 2 and J equal to 3 suppose I have mentioned the prototype like this ok because of there is a rule the default parameter should be initialized at the time of function declaration it is the rule for functions with default arguments that's what happened at the time of declaration I have initialized X with 1 Y with 2 visiting now what happens okay now when the function is called what happens when the function is called first it 10-20-30 r+ XY jet already XYZ are having what 1 2 3 but matching values are fauna that's why here 10 is replacing X 20 is replacing Y 30 is replacing yet the Phi X value becomes 10 y value becomes 20 yet value becomes 30 + y total result 60 that's why this function result is 60 okay now the first function calling completed second now second function calling what happens 10 goes to X going to wonder a placer with the tang and to replace rate to 20 now JD is missing here the corresponding value misses for jelly okay when it is missing automatically or compiler replace it with 3 that means here X to the constant Y becomes 20 but Jade becomes 3 that's answer 10 + 20 + 3 that is why second program result is 33 okay let's come to the total sum of 10 now 10 is possible to X but there is no matching value for value n digit now why energies are officially 203 that's why your program becomes x value 10 y value to judge value 3 the first 10 plus 2 Plus 3 answer 15 okay the first 10 passes to x + y - value positive aren't clear now so 10 plus 2 Plus 3 answer 15 okay now what happened last function call here there is no matching orbit if we are not sending any argument that's why all the parameter values are missing now automatically excited or replaced with 1 2 3 that's why this formal parameter values becomes 1 2 3 1 + 2 + 3 on series see now you know physics that means according to this example okay according to this explanation C++ allows to define a function without passing or to call your function without passing all the argument okay here the concept is very clear okay C++ ellos to call your function without the passing all the argument which is called functions with the default argument and when it is useful for example I am giving small example suppose they ridiculous in class okay just think C++ class it is and sixty students are there and generally every student is having ID number it is common thing and every student is having a name it is also common and every student how to generally course also for example one student number one second student number two that means the roll numbers are going to be changed for each and every student and names are also going to be changed but the course is say now suppose if the facility is not available if this feature is not available then every time we have to send the course C++ that means how many students we are having 60 students we are having that means we'll have to send 60 times the safe list length in place of course that's why your program is getting little bit too lengthy okay now instead of that one we can use the default argument suppose it is the ID number it is the name it is the course here just initialize the course with C++ for example now with all the course details automatically they are replacing with the shapelessness that means we have to send only one shapeless place instead of 60 C++ that means your time is favored program also looking to be smart yes program minimizer pen program is minimized performance automatically increase it is the concept of functions with default argument and it works only in C++ okay it works only in C++ compiler see never suppose functions with default argument and Javed never supports functions with default argument but dotnet supports function with default arguments concept it is how the functions with the default argument is working next I'm going to give one more example and before going to give the second example I'm going to say one more thing here see this it is looking to be functions with three argument it is looking to be function with the two argument function with one argument function with normal means the same function but which is easily in four different manner which is called function polymorphism okay that's why functions with default arguments suppose the concept of function polymorphism means using the same function in different ways it is the function polymorphism concept which is introduced it is with whoops concept in sleeplessness that's why we can define several functions with a different argument it is called functions with a default argument and here certain rules are there for functions with default arguments now what is the rule first thing the functions with default arguments okay in functions with default arguments the value should be sent from right to left remember this the value should be provided from right to left for example this one okay X is missing the value Y value is there yet values it and you are having only one problem here this function is not working because of here ten past two x here ten pass of x here ten percent it's already y&z they are having default água but here there is no value for that only in this situation is giving error but here watch it suppose x value 1 now you are missing to be here now it never supports because of it is a against the syntax rule because of in functions with the default arguments the arguments should be submitted from right to left that means revolution is from right to left and take in receiving it from left to right that's why in middle in middle don't enter the default argument okay you have to enter the default arguments always from right to left only left to the right not alowed in middle also they are not allowed it is how to work with functions with the default argument now I am going to give one more example small example on functions with the default organized okay and one more thing here we have to determine in previous example actually we think that it is AC program that's why we have started with test e di u dot H okay but actually we have conducted that example in C++ environment okay and there I have user see out object that's why English off as tdiu dot H you should have to provide iostream dot H okay here it is the important thing okay we have discussed about seeing enrollment later I have given in illustration where on when you are working with C++ with the field you should have to provide a header file I will scream dot H next now I am going to define one more thing say this suppose float simple interest I want to find out the simple interest generally simple interest formula is what PTR by hundred now principal time rate of interest okay here I am going to write like this float P V stands for principal amount I Here I am not providing any value and float supposed time okay here I have ended float time next float are our means rate of interest suppose I have interest rate of interest teeny now brackets close here watch it first two arguments are left blank but third argument is replaced with the T okay now what happens a return T into R by hundred actually formula is implemented okay now the function declaration and definitions selection later I am going to use the function like main how to calm a function next CEO directly I'm going to write you simple interest equal to suppose simple interest off causal principle about it is possible to pick next time Hubble's 10 months next rate of interest here - now thousand is replacing the p + 10 is replacing the t + 2 is replacing the three okay fine three arguments are submitted and it is going to give us some basil next watch it see out simple interest equal to simple interest of same thousand rupees but this time only 10 okay here what happened here what happened totally ratio to buy p no okay - is it p value becomes housing next 10 pass it would be p value becomes 10 on to posit to our means to be refrigerated - okay our value replace ritter know what happens 10 months okay in tough said to here seven hundreds are there in this example in for 100 two rupees no 10 hundreds were to induce 20 like that 10 month that's why total answer 10 into 20 200 is the simple interest that's why this program gives answers 200 rupees simple interest 4000 rupees okay next watch it here second example thousand rupees refused YP okay fine p value becomes close it next p value becomes 10 now what about the R value here we are not providing any value for R then what happens here default argument is in now it receives 3 that's why our value becomes three now three rupees interest like good 10 months four thousand hundred rupees two rupees like ten hundred swords in adultery Scottie 13 to 10 300 that's why he has simple industries can into 30 that's why it becomes 300 now it is the simple interest okay now watch it here for having clear good but second one is having forward now the third argument automatically receive it it three it is the useless or four functions with default arguments in our regular examples okay it is how to work with functions with default arguments thank you for watching okay for more videos subscribe nourish shade [Music]
Info
Channel: Naresh i Technologies
Views: 68,582
Rating: undefined out of 5
Keywords: Function, arguments, default arguments, C++, Function with default arguments in C++, C ++ Tutorial, Kishore, function, programming
Id: uxhaXnk3FOs
Channel Id: undefined
Length: 21min 35sec (1295 seconds)
Published: Thu Mar 09 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.