Defining a Function Prototype and Definition - C++ Tutorial 12

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome everybody in this episode we're going to learn how to create our own custom function this is going to allow us to extract some section of code invoke it by name and we can do that multiple times if we ever need to change that code we only have to change it in one location versus throughout all of the code so this is a really good way to improve the quality of your code reduce typing and yeah you just you just do this when you're programming so we're going to learn how to create functions if you've enjoyed the series so far then you will definitely enjoy C plus Builder this is the IDE I've been using for this series check out the link to the free Community Edition down below definitely would appreciate it if you want to support this channel now let's get to creating functions so let's say you had some structure for your code and you wanted to keep it pretty much the same but you wanted to basically say hey once you choose a difficulty just play the game so let's go ahead and condense these cases it's going to be the same regardless if it's easy medium or hard what we'll do is we will just say case 0 1 or 2 which is easy medium or hard and in here we could just say play game and then break so we basically extract the entire game functionality and call it by this name and you could do something similar if you wanted to have like an impossible mode that was very very difficult and you had to have beaten the game at least one time you can ask hey have you beat the game and if yes if beat game is true we can do the same exact thing here play game without the ability to invoke a function like that we would have to copy this Behavior two times so now we can Define play game in a single location and then invoke that anywhere we want a few other changes is first we're not going to have to require them to enter some secret passcode so we can just remove this line here so it'll just assume in this situation they will want to play impossible mode we'll say play game and later we can possibly pass in like a difficulty or something like that we'll talk about those options the other thing is after this is done it's going to just continue on with this code and ask them what difficulty they want which doesn't make sense if they're already playing the game so you have the option to say return zero here basically copying that final code down here you'll also need that system pause if you don't want it to just automatically close the other option would be to just use an else here so we can say else and then we will surround everything else in curly braces so it'll look like this and then I'll just go in here and indent everything inside of these curly braces so I think that is right the only problem now is that this function we're calling play game it doesn't yet exist so we're going to Define it so oftentimes you will see it set up this way where you have a function prototype void play game it'll look like this now I'm going to show you a different way I prefer but for now you will just say what the name of the function is and its return type in this case void meaning we're not going to return any data back to the user or I guess instead of user I should say the developer so the person who is invoking this function they're not going to get any data back so after main you can scroll down to the bottom that's we're going to put the function definition so we'll say void play game and this will actually have the implementation so you're going to have the curly braces and just to check that this works we will say C out playing game dot dot dot new line end quote semicolon so let's test this out we will play do you want to play a game yes have you beat the game yes because you beat the game you can play impossible mode playing game so you can see it actually did make it down here to this function call and outputted that let's go ahead and try it again this time not on the impossible mode setting so have you beat the game no we will go for medium and it says playing game so either one plays the game we don't have any way to configure difficulty or anything yet but this is a start now I think having the the Prototype at the top and the definition at the bottom is kind of redundant when you're just doing everything in a single file like this so what I'll tend to do is cut that and bring it all the way to the top where we have the Prototype and just replace the Prototype with the definition itself and in this situation the definition acts as the Prototype and the implementation so this should work just the same you can run the code and it works yes I want to play no I have not beat the game and I want to play on easy cool you can also see that we're kind of taking advantage of that fall through capability of the switch where we're basically doing the same exact thing for easy medium and hard you could of course go in here and do different things for easy medium and hard that's totally up to you now I want to talk about arguments which is going to be the focus of the next video because right now we have no way to pass in some kind of difficulty when you extract functionality into functions you'll often need to pass in data known as arguments so that's what we're going to be talking about as well as return data in The Following episode if you've enjoyed this content so far please be sure to give that subscribe button a little slap and check out for the next episode [Music] [Music]
Info
Channel: Caleb Curry
Views: 6,594
Rating: undefined out of 5
Keywords: c++, cpp, c plus plus, intro, introduction to c++, c++ beginner
Id: s5K7EPvuI_k
Channel Id: undefined
Length: 6min 29sec (389 seconds)
Published: Thu Dec 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.