C++ Programming Tutorials - 33 - C++ Functions In Separate Files - Eric Liang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is up guys my name is Eric and ended past I taught you about functions in C++ now in one of the few tutorials I've taught I probably mentioned that functions can be separated into its own individual files however the reason I did not do that and I kept it all in one file is for the sake of screen recording and all that stuff just to make things easier to navigate without having to switch screens all the time however that is not a good practice so that's why in today's tutorial I'm going to be teaching you how you could separate functions into it their individual files in C++ don't worry it's really easy okay so if we were to use our previous example which is episode 32 on creating recursive functions in C++ we created a simple calculator to calculate the factorial of a number now as you can see here we have a factorial function of type double which basically returns a data type of double now here you can see there are two functions inside the main CPP file there is the factorial function that we created on our own and there is the default main function that is required in all C++ programs now in order to separate our factorial function from the main dot CPP file into its own file this is what we have to do first off inside your source files folder as you can see in the right column here right click the source files folder go to add new item and then select the C++ file which gives you a CPP extension now it is good practice to name your file the same as the name of your function so in this case because our function is called the factorial function we'll call it factorial dot CPP ad and now one more thing that we would have to create is inside the header files folder which you can see in the right column here right-click it go to add new item this time instead of clicking the CPP option you're gonna choose the header file option which has an extension of dot H again the same concept applies use the name of your function so in this case it's called factorial H ad okay now if we're to go back to our main dot CPP file copy this delete the code and then go to your factorial dot CPP and paste that code inside of it now on top of the file type in include iostream and using namespace STD and then save okay so as you can see if we were to go back to our main dot CPP file it shows a red squiggly line under the factorial function call so if we try and compile and run this program it should not work and as you can see it says there are build errors now this has to do with the fact that because the function is no longer inside the main CPP file however it's still inside our project folder but we haven't included the factorial function into our main dot CPP file so that means the program does not know where the factorial function is so in order to include it into our main dot CPP file this is where the factorial dot H file comes into play inside your factorial dot H file there should be something called pragma once automatically generated if not type it in and then below it inside the factorial H file type in your functions name and return type and any parameters that goes inside of it so in this case because our factorial function is called double factorial parentheses integer X that's what we're going to be typing into our factorial H header file so type in double factorial parentheses and then inside int X and then end it off with a semicolon and then save the file go back to your factorial CPP and then underneath or above the aisle stream typing the line include and then in quotes type in factorial dot h save the file now go back to your main function and do the same so type in include double quotes factorial dot H now if we're to try and compile and run this program it should work and as you can see the red squiggly line went away so click yes to build it as you can see voila the program runs successfully and it shows the result of 2 point 4 3 2 9 e + 18 perfect so as you can see that's how you separate your functions into their own individual files in C++ so it's a quick refresher before the function was in here but ever since then we moved it out created a CPP file as well as a corresponding header file inside the CPP file add in your usual include files using namespace STD followed by the function that you created and inside the header file you type in the double factorial index and then semicolon and inside both of your CPP file remember to add the line to include your dot H file that you created and make sure the name is in double quotes instead of angle brackets and then from there if you were to try and compile and run this program it will work perfectly and that basically concludes our tutorial on how to separate functions into their own individual files in C++ thank you for watching and if you like this video please be sure to hit the thumbs up button leave a comment below if you are still confused on anything mentioned in this tutorial and if you loved this video series be sure to subscribe and I'll see you next time [Music]
Info
Channel: Eric Liang
Views: 12,250
Rating: 4.8883719 out of 5
Keywords: C++, C++ programming tutorials for beginners, how to program in c++, introduction to c++ functions, functions in c++, c++ functions, functions in separate files, eric liang, c++ visual studio, C++ programming for beginners, c++ programming
Id: 47sZhrJ1USY
Channel Id: undefined
Length: 6min 7sec (367 seconds)
Published: Thu Aug 31 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.