In this lecture, we'll start learning how we can use functions to decompose our complex programming problems. Before we can do that, we need to know what a function is. So what is a function? A function is a reusable chunks of code that we can use to solve a small problem. This is problem decomposition at its finest. We can take a bunch of these small sub-tasks functions and use them to solve more complex problems. To actually make a function execute to run it, we have to call it, and we'll see how to do that in this lecture. We've been using functions for a long time. We've used functions from the C Standard Library since our first programming problem, fgets, printf, and scanf, are all functions. They just happened to be functions that somebody else wrote and we just used. It turns out that we can also write our own functions, and that's what we're going to do in this lecture. Our starting code just prints a welcome message, so let's go ahead and run that code, and as you can see, it's welcome to the amazing first function program, and its a lie at this point, because the welcome message wasn't output by a function. Let's change that now. The way we write our own functions, is we go past the end of the main function. Remember this is a function as well, it's the standard function we need to use as the entry point for our C programs, but we can write as many functions as we want. In this lecture, we'll only write one, but as time goes by, we'll find ourselves writing multiple functions of our own. We go past the main function. The first thing we put is the return type for the function. We discussed way back when that the main function returns an integer and exit success is zero when everything went okay, and some other number often negative one when something has gone horribly wrong as you run your program. But this hint says that the main function returns an integer when it's done. We are going to write a function that prints the message, and that function doesn't have to return anything. We use a special keyword called "Void", that says, "I'm not going to return anything. I'll just do my work as a function and be done with it". The next thing that we have is printMessage, the name of the function. Just like main is the name of the function here, printMessage is the name of the function here. The next thing, which we can see as we look at the main function, is we have an open parenthesis, and then a list of parameters information that gets passed into the function for it to work properly, and then the close parenthesis. The printMessage function won't need any information passed in. We still need the open and close parentheses, but we don't have to put anything between them. Finally, we put the function body, which appears between curly braces. Of course, because I'm a good programmer, I'm going to comment my function as well saying what this function does, and I'll say, "It prints a message." Next, I'm going to cut all this code that prints the message out of the main function, and I'm going to put it into my new printMessage function instead. I'll build to show that there are no compilation errors, and now I'll run it. This is disappointing, like nothing happens, here's the thing, even though we wrote this code, we never called this function from anywhere, and as I said in the introduction to this lecture, we have to call a function to actually make it run or execute. That means, here I need to call printMessage. Like that. We've been calling functions, as I also said in the introduction, lots. We know we put the function name, we put an open parenthesis, if we need to provide information, we provide arguments, so when we're calling a function, we call those things between the parentheses arguments, and when we're here on the function side, we call those things between the parentheses parameters. What of programmer's, will just call them perimeters everywhere, and other programmers really understand what they're trying to say, but the right terminology is arguments when we are calling, parameters when we get called. I'm going to build again. You can see, I get a compilation error. We can also look at the warning. The warning says that printMessage is undefined, and here's what leads to the error. The warning says we haven't defined printMessage, so it assumes that it's going to return an int. Then when we get to print message, we said it returns a void, so it says different basic types. Let's fix that problem. The issue is that the compiler starts at the top of our program and works its way down, and it gets here first. It gets here before we've actually defined the function, so the compiler says, "Well, I don't know what this printMessage thing is. So, I'm going to assume it's some external function that returns an int." Then it gets here and says, "Well, that doesn't match my assumption," so we get the compilation error. The way we can fix this problem, is we can tell the compiler before it gets to the function call about this function. Of course we don't want to define the entire function, we just want to let the compiler know that there is in fact a printMessage function that returns void, and be patient, you'll eventually get to it, but we need to give it enough information so that when it gets here, it knows that this should compile properly. Specifically, we need to tell it it's called printMessage, and that we don't have to provide any arguments when we call it, and this one is void, so we can't be trying to put the results or calling this function into an integer or something like that. All the information we need is right here. Everything except the function body is exactly what the compiler needs to know before it gets to the first call to that function. I'm going to copy this information up here. This is called a function prototype, it's everything in the function except the body, and we end it with a semicolon not with an open and close curly brace with a bunch of code in between, but we need to do this to make it so that our program compiles. Some compilers will let you get away with not providing a function prototype, and others won't, so it's best practice to just always provide function prototypes for all the functions that you've written. Now when I build, I get a build succeeded, and even more exciting, when I run, I get that message again. So how does this all work? What happens is, as we've discussed in the past, the operating system calls main. The first thing that happens in main is we call the printMessage function, so we go to the printMessage function and do each line of code in that function, and when we are done, we return to the line just after the function call. Of course, that could be another function call or lots of other stuff, but the execution of the program whenever we call a function we go to that function and do the stuff in that function, and then we return from the function just past where we left. So there you go. That's our first function. To recap, in this lecture we learned what functions are, and we wrote our very first function.