Last time, we wrote our very first function. This time, we'll learn how to write a function that returns data to the caller of the function. Before we do that, I know that the function we're going to write is going to return a random number to whoever called the function. So, let's go look at the C standard library to see if there's anything there that can help us with this task. If we look through the header files in the standard library, we can in fact find standard live.h has a pseudo random number generation functions. So, let's take a look at those. There are two functions available here. There's rand, which generates a pseudo-random number, and there's srand that initializes a pseudo-random number generator. So the way pseudo-random number generators work is that we pseud them with a value and then they use that pseud value to algorithmically generate a sequence of pseudo-random numbers. They're called pseudo-random because given the pseud value, we know exactly what the sequence of pseudo-random numbers will be, and so we can't in computers achieve pure true randomness, but we can achieve close enough for pretty much anything we really need to do. It turns out that the sequence of things we should do is first we call srand to initialize our generator and then we call rand as many times as we want to get those random numbers, and from now on I'll call them random even though they're pseudo-random. In our starting code, I've pound included this time.h and you'll see why soon. What we're going to do is we're going to initialize our random number generator using srand, and then we're going to generate numbers and we're going to do that by calling a function that we implement ourselves. We know we should provide a function prototype, we'll make our function return an int, and we'll say get random. We won't have to pass anything in, so I don't have to put anything between the parentheses here. This warning by the way says, we don't have the function definition yet and we know that because we haven't written the function yet. Let's go write that function now. It returns int and you may find it's actually easier to just copy the function prototype down here so that you get it exactly the same. That's important for the compiler that you get it exactly the same, and I'll provide the comment that says generates a random number. We know that we can just call the rand function to do that, but this generates a random number and doesn't do anything with it. We said that we would return an integer from this function. That means we need to say we're going to return something and the something we're going to return will be that random number that we generate. We didn't need to return anything when we implemented the printmessage function in the previous lecture because remember this set void for our printmessage function. So void means I don't return anything, so of course you don't have to. But if you say you're going to hand back an int, then you better somewhere in your code return an int. Up here, we'll initialize our random number generator and we know that we call the srand function to do that. Our next question is, what should we provide as our pseud for the random number generator? It's really common for people to use the current clock time on the computer you're running on as the pseud and the way we get at that is by calling time zero. It turns out that to do this, I need to pound include time.h. So that's why I pound included time.h there because I wanted to get the current clock time down here. All right. Let's generate some numbers. Let's generate five random numbers. I'll start at one and I'll printf Random Number %d. So this will be the index. This is a random number one, two, three, four, five, and this will be the actual random number we generate. That means that we need to provide two values to match up with these format specifiers. The first one is i, and the second one is the result from calling our getRandom function that we just implemented. We didn't see this previously. We had a function called just standing alone, but we can actually put a function call inside another function call that's fine. It will do this first before it does this one. It will start processing this one and it will say wait a second, I need to go get that random number and it calls that function and then once this returns an int, then it can continue with the printf. Let's add a new line and go ahead and run this. There's a set of random numbers. I'll run it again. I hope you memorize the previous random numbers. You didn't have to. Trust me, these are different random numbers. Now, they are different random numbers, but because of the way random number generation works, some of them could have actually match. If you're flipping a coin it's possible to get two heads in a row, so randomness doesn't guarantee uniqueness. I will confess this is a pretty simple program. We could have just put the call to rand right here instead of writing our own function and this would have worked the same way that we just observed at working. The reason we did it this way though is because I wanted to show you two things. I wanted to show you writing a function that actually returns something, and I wanted to remind you that because functions are reusable chunks of code, we can call a function multiple times in a program. In the previous lecture, we only called our printmessage function once, but here we're calling getRandom five different times as we go through the loop. So that's a reminder that functions are reusable and we can reuse them as many times as we want. To recap, in this lecture, we learned how to write a function that returns data to the caller of the function and we learned a little bit about randomness as well.