What are Functions in C++ | Easy and epic explanation 2021

Functions in C++

I say functions in C++ I’m explicitly talking about something that isn’t part of a class it’s pretty common for us to split up functions to prevent collected location we want to be running the same card multiple times because of course if we did a pop from coughing and pasting a lot of carton just ending up with this huge mass. It also means that if we decide to change some card we have to change it and all of those places where we pasted the original card and that’s just going to be a disaster to maintain so what we can do instead is just right an awful function that does what we want to and then we can call it multiple times now card if we need to you can think of functions having an input and output although they don’t necessarily need to we can provide the function with certain parameters and the function can return a value for us. functions in C++ functions in C++

So I suppose that we wanted to multiply 2 numbers together and we want to try to function that did that so the first thing I’m going to write here is something called a return value that is this is the type that is functional time system multiplying 2 integers that of course will result in a ditch outside our tent it’s going to be in I’m going to give the function name in this case multiply. And it’s going to take 2 parameters these are actually the numbers that we want to multiply together functions in C++ I’ll just call in pay and pay I’ll give the function of body and all this is going to do is return Hey time space you can say that we’ve got a function here that takes in 2 parameters both indigenous and simply returns the product of those 2 numbers we don’t necessarily have to provide parameters for example I could just not provide any promise and or 10 something like 5 times right this is still a function that returns an integer but it’s just not taking any prominence we can also tell the function that we don’t want to return anything and we do that by writing void as for 10 time void of course means nothing instead this could do something like love the results to the console.  functions in C++ functions in C++

So let’s go back to our original example here what we had in Spain in Spain and we were times the product of those 2 inches so how do we call this function well calling a function is pretty simple let’s go ahead and try and print the result of a multiplication I’m going to first of all make a variable which holds this results of all time in result equals multiplied I will go with 3 and so what this is going to do is call this multiply function with these 2 parameters and then store the return value that is this result of a times B. in this result integer we can then output that results through the council let’s hit F. 5 to run a program and offered bills you can say that we get 6 which of course is what 3 times 2 is so let’s kick this up a notch the person I want to do a bunch of multiplications and I want to log into the console if I do something like that without a function in it with the pretty messy so for example I need to repeat the card so let’s go ahead and copy and paste a few times I’ll call if something like resulted result 3 will do 8 times 590 times 45 if I run my program and hang on a minute let me getting the same value everyone a look. functions in C++ functions in C++

when I covered by this card I forgot to change the variable now you may have thought that I did that by accident but I actually did it on purpose to prove something this actually happens all the time if you copy and paste blocks of card and then forget to change 1 minor detail and in certain situations you might actually just run your program and not even notice is not working correctly until it breaks somewhere down the line and yet something like this can actually be fixed really easily if you just credit function for let’s go ahead and fix this by actually printing out result 2 and 3 if I run this we will get our correct results which is great however you can see that I’m actually calling this multiple times and it’s just a little bit annoying like for example further down the road if I decide to replace this multiply function by simply doing something like 8 times 5 days after place it in every single place 3 times tour 90. MS 45 that’s I don’t have to deal with that so this multiplication and then logging the result let’s go ahead and make a function for that it’ll be void because it’s not really going to retain anything to us it’s just going to perform what we ask it to do local the something like multiply and log and then let’s take a look at which parameters we might want so what actually changes between these 3 blocks of cart the values that we actually multiplied that’s it. functions in C++ functions in C++

So does become the parameters for our function what actually changes between is also part of what needs to be specified for this function to perform its job let’s go ahead and write in apparatus so I going to be taking in 2 inches and 3 you can really hold anything you want but I am basing sensible will copy and paste one of these blocks into this function this looks pretty good of course our place 3 into without parameters so that way using the promise we specify into dysfunction before the multiplication against which will cause I times PDF multiplied here and then we’re going to be looking at results the consul so now instead of doing this so many times all I have to do is simply call multiply and log. With my prime minister 3 into for example and then we have 8 and 5. And then we have 90 and 45. And that’s it look as I can get rid of all this card and this is what we end up with a nice clean and easy to rate program if I launch my program you can see that we get the correct values yeah so this is a pretty simple example but I think it’s effective in demonstrating that functions are really important you should be aiming to split up according to many functions. functions in C++ functions in C++

However one thing that I want to stress is darker overboard your need to function for absolutely every line of cart that’s not going to be good for anyone it’s going to be hard to maintain your client’s going to look messy and cluttered and it’s actually going to make your program Slovak every time we call a function asterisk the compiler generates a cold instruction what this basically means is that in a running program in order for us to call a function we need to create the entire stack frame for the function meaning we have to push things like the parameters on to the stack we have to also put something called a return address on to the stack and then what we do is we actually jump to a different pot about binary in order to start executing the instructions from our function and the return value that we push we need to get back to where we originally what before we call the function so this whole like jumping around memory in order to execute function instructions and all of that takes time so slow down a program now the reason I said asterisk Elia was because this is all assuming that the compiler decides to keep out function as an actual function and doesn’t in line that we’re going to talk in depth about in lining in a future article so the reason I’m saying all. functions in C++ functions in C++

This is because you don’t want to just go ahead and credit functions absolutely every line of car don’t be ridiculous about it takes a little bit of experience to realize what you need to function for but basically if you see yourself doing a common task multiple times credit function for that the primary point of functions is to prevent code duplication we don’t want to just be copying and pasting card everywhere now if we go back to my card for just a second you might have noticed something a little bit about this main function it says that his return values into however the return K. what is nowhere to be found and I’m obviously not returning anything so if I specify a return value do I actually need to return something let’s go ahead.  functions in C++ functions in C++

I’m trying just do nothing in this multiplatform channel had control of 7 to compile look at this I’m getting an error telling me to multiply must return a value so do functions with the return type actually need to return values the answer is yes they do the main function is actually a special function the main function and only the main function is exempt from this kind of must return a value if you don’t specify return value it will automatically assume that you’re attending Sierra so will be identical to if I had written this this is just a feature of modern seems impossible specials that lets you just keep a card a little bit cleaner and just have fun no this must return a value thing is actually something that only applies in the markets we can follow release mark yeah you’ll see that we actually don’t get an error that’s not to say that what we’re doing here is correct because if we actually do capture that return value decide to do something we will get on the phone behavior it’s just that the compiler one actually yell at us however in day backward with certain debug compilation flags enabled we will get an arrow which will just help us develop a card because at no point should you be writing a function that says it’s going to attend something but doesn’t okay so that’s pretty much a basic introduction to functions are really useful. functions in C++ functions in C++

Also See

How to Setup C++ on Windows | Top Latest 2021

Leave a Comment

Your email address will not be published. Required fields are marked *