 Hey everybody, it's Brian, and this is our, oh gosh, our sixth C++ video, we're already up to six. I'm gonna do something a little unconventional, I get really, really tired of watching tutorials, and they just go over the same thing over and over again, and they leave you asking a lot of questions. So this one may seem a little different, but I'm gonna try and teach you two things at once. So what we're going to learn today are functions and header files, and why you need both. So what is a function? Well a function is a block of code. For example this int main that we've been working with this whole time is a block of code. That's a function. It just happens to be the function that the operating system calls when the program's loaded. So as you can guess, we can just make a function. We'll call it getH, return, and I'm 36, so we'll just say 36. So we have a function called getH. It returns an int, remember our main function returns zero, but we're just gonna return an age using the return keyword. There's my age, and here's the block of code that's gonna get executed when we call getH. Now there's a little trick I'm gonna show you. Remember a couple tutorials ago I said C++ compilers were incredibly stupid. I'm gonna prove it to you right now. You ready? So we're going to say int age, and then we're going to say age equal getH. And we're just gonna say, excuse me, cout age. So we're just going to print the age. As you can see our function returns 36, so this should print 36. Anybody wanna place some money on this? Ooh, there's some build errors. Get age identifier not found, but it's right there. I mean we can see it. What's the problem here? Well if you're looking at this, think like the compiler and read it from top down. You go include using namespace int main, get age. There's no get age so far. So the compiler's panicking because it doesn't know what get age is. So there's two ways around this. You can either cut it and paste it above the main and then it'll compile and run perfectly fine, say 36. Or you can do what's called a function prototype. Now a lot of the books and classes you'll take will tell you what function prototypes are but won't tell you why you need them. So let's just create our function prototype here. Now it's called a prototype because it has the signature of the function. You can see it looks exactly like it but it has no body, no code block. It's missing the things in the brackets. So all this does is telecompiler, hey there's a function out there called get age that returns an integer. Doesn't care what it does, just defines it. Now you run this and as you can see it compiles perfectly fine. Same result. Now why do you need function prototypes? This is what a lot of books and classes leave out. And it leaves a lot of people new to this language struggling with why do you need this? What purpose does it serve? I just paste my code up here. Well let's say this file is getting big. I mean we're talking thousands of lives of code. You want to break this up into several files. So we are going to just add a header file and we will call it test.h. And you see we have our separate file here. And what we want to do is take this code, cut it and paste it in here. Now we have to have some way of calling that because if we try to run this as is it's just going to explode and go what's going on. You get these error link and the number and unresolved externals. It doesn't know where get age is located so you have to include your header. Her include is a preprocessor directive which basically says include the code at this location. Now you notice something strange here. Include iostream, this is where we get C out and C in, has got these brackets. But I've written test in quotes. Why is that? Well typically things that are part of the standard library you will do in brackets and things in quotes are libraries that you've written yourself or that you've gotten from another person. It does that to kind of help the compiler weed out and figure out where these are actually located. And then it has this whole search order that it does. I won't really get into that. It's kind of compiler specific and operating something specific but generally if it's in the same directory structure it will find it pretty quick. Now compiles, links, runs, gets the result even though it's in a separate file. So that is why you need a function, help if I can spell it, function prototype. So you can put the code in another file and that breaks out your program very easily. Now let's make another function real quick. I want to show you something. Let's actually throw it in here. Let's say void, say hello. Now void means nothing. Void tells the compiler that say hello is going to return nothing. And let's say see out, hello. Now can you spot the error in this program yet? I'm going to go ahead and run it just so you can see what happens. But this is another common mistake when you start working with header files. There were build errors. Would you like to continue? No. Undeclared identifier. Now we've run into the undeclared identifier before. It means we simply haven't declared the compiler has no idea what see out is. But we've got all this stuff over here. Remember, C++ is incredibly naive and you need to tell it exactly what you expect of it. So what it's looking for is the include IO stream and the using namespace standard library. So we're just going to copy this, paste it. We don't want to include test.h again. So now it should compile and run just fine. Sure enough. There we go. So just, I know I threw a lot of AIS. We're going to review really quick here. We covered functions. Remember the main, the entry point of the application is a function. We covered function prototypes and why you need them. How to include header files and how to create your own. And remember, when you create your own header files, you should always add your includes and create it like a totally separate part of the program because the compiler is not going to know where things are. Well, this is Brian. I hope you found this video educational and entertaining. And thank you for watching.