Transcript for:
C++ Functions - Introduction

hi everyone welcome to my channel welcome to the first video of c plus plus functions if you're a beginner at c plus plus make sure to check out my playlist on c plus for beginners and there you can find some of the most important concepts that you will need to understand and know in order to work with c plus and in this video we are going to talk about c plus plus functions so functions are one of the most important concepts that you will need to understand and use in order to be a good programmer and that is not only case in c plus plus but in most programming languages as well and before we start make sure to subscribe to my channel and also click the bell icon so that you are notified when i publish my next video and also if you are interested in how developer life really looks like and if you want to see some of the behind the scenes of these videos that i post on youtube make sure to follow me on my other social media platforms so instagram and twitter accounts through code beauty and without further ado let's start with this video the first thing that i want to explain is what is function function is a block of code that is grouped together and usually that block of code solves a specific problem meaning it performs a specific task and that block of code is executed only when you call that function meaning only when you invoke the function the body of that function is going to be executed and each c program contains at least one function and that is going to be main function so this here and the execution of your program starts on the first line of your main function and then it ends when it comes to this last line so this close bracket of your main function and also it can end when it comes to return statement but we are going to talk about return statements later in this course so main function is the main one so the first one that is executed and whatever code you put in your main function that code is going to be executed as part of your program so if i say here for example c out and i say hello from main function like this and let's add a new line like this and now if i run my program you can see that our program has written out hello from me and that is the first function that our program starts executing besides from main function you can also create your own functions and let's explain how you can do that so there are a couple of things that you need to know in order to create your own function and that is going to be following so the first thing that you need to really write when you create your own function is going to be return type of your function and that is going to be void for now which means that this function is not going to return anything and we are going to talk about return types more in detail later in this course so for now we are going to use void return type meaning that this function that we declare in this line it's not going to to return anything and then you write out the name for your function and let's call it function like this and then in these parentheses you can put arguments or parameters that your functions that your function really receives and for now these parentheses parentheses are going to be empty meaning that this function does not receive any arguments and then in these curly brackets we are going to put the body of our function so this here is going to be the function that we created ourselves and you have to specify specify the return type of your function after that you write out the name of your function then in these parentheses you can put any arguments that your function receives in this particular case these are empty and we are not going to send any parameters to our function and then here is going to be the body of your function and what we are going to put in this body let's say c out hello from function like this and let's also add end line at the end of this function so now we have really created our own function which is called function and if i run my program now what do you think that is going to happen write that in the comments down below so i am going to run this program and as you can see nothing has changed really our program has written out hello from main as it did before so before we created this function that is because as i explained your function is not going to execute until you call that function so until you invoke it it is not going to execute so in order for our function to be executed we need to invoke that function and how that is done so here in this line after hello from main we are going to put function and then these parentheses so this here is the invocation of our function and now our function is going to be executed so now if i press this play button you can see that now our console has changed so now we have hello from main and then also hello from function so that is because we have invoked this function here and you can invoke this function wherever you need it so you can i have invoked it after this line here but you can invoke it as well before this line here if you need to and if you want to so now if i run my program you can see that at first says hello from function and then it says hello from main because this function has been invoked first and then we have written out this line of code here and that you can see in this console window and let me show you one thing so i'm going to return this after this line of code the location of our function i'm going to return here and now i'm going to take this code here so this definition of our function and i'm going to paste it after our main function and let's see what is going to happen now so let me delete these these blank lines and now if i run my program you can see that our program has failed to build so we have compile time errors and in this window here in this error list you can see that it says function identifier not found which means that this function here is really our compiler does not understand what this function is because c plus code is executed from top to bottom and when it comes to this line here so when it comes to the invocation of your function it is really not familiar with this function it does not know what this function is so you have to either put this code before your main function as we did before or you have to do other thing which is really the recommended way to create your functions and that would be to create definition and declaration of your function so what our definition and declaration of function declaration of function is going to be really this here so i'm going to copy this and put it before our main function and this declaration of function is going to tell to your compiler what is going to be the return type of your function what is the name of your function and then if there are any parameters that your function really receives you are going to put those in these parentheses here so that is going to be declaration of your function and that part declaration of your function goes before your main function and after that you are going to write the definition of your function and the definition of your function is going to go after your main function and in that way you are going to make your code more readable meaning that when someone else comes to your code and if your code had 200 or three or 400 lines of code then it would be really hard to to read that code and for someone it would be really hard to manage and go through your code so he is going to find it much easier if he has all the declarations of your functions before your main function and then if he needs to read a specific definition of a function he can right click the name of that function and click go to definition and then he's going to be taken to the definition of your function so the specific one that he needs to read so that you don't bombard um that user with all the functions old definitions of functions that you have in your program and in this particular situation it is only one function but your program is going to consist usually of more than one function and that is really unreadable when you find a lot of definitions of a functions before your main so you can right click on your function and say go to definition or you can press f12 so you can click on your function and then press f12 and you are going to be taken to the definition of your function okay so this here is the recommended way to create functions and now if i press play button you can see that our program has executed actually has written out the same as it did before so we have uh really removed that error that we had when we moved this definition of a function after our main because we have added this declaration of a function before our main function and there is really the most important thing that i want to mention in this video and that is going to be what is the reason why functions exist okay so we have seen that they make your code a bit more readable so they group the parts of your code together so that you can manage your code easier and the most important characteristic of functions is that they make your code reusable meaning that you can write a specific code once so you can solve a specific problem once put the solution the algorithm the code for that problem in a function and then you can really invoke that function however many times you need it so here i can really copy this and then paste it let's say three times if i needed it and then if i run my program you can see that this function has been executed three times here and that is really the most important thing about functions which means functions make your code reusable you don't have to write the same code over and over again and you should really try to group your code in functions so that each function does a specific task nothing more than that and in that way you are going to make your code easier for yourself and for other people as well that are going to read your code so i hope that you understood what functions are and how they are used and we are going to talk about functions in more detail in my later videos so make sure to subscribe to my channel and click the bell icon because only then you are going to be notified when i publish my next video and also follow me on my other social media platforms so instagram and twitter accounts through code beauty thanks for watching and i'm going to see you in my next video bye