What is a function prototype in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're talking about function prototypes what they are and how to use them in your C and C plus plus programs so let's jump into it [Music] hey welcome back everybody thanks for being here we're talking today about function prototypes which are something that are it's very basic many of you I'm sure have already seen these before but it's something that new programmers in C and C plus often see and it often gives you a little bit of confusion until you figure out what it's all about why it's there and so today I thought we would just do a beginner video to talk specifically about prototypes how they work what they are and how you can use them and so they no longer give you any confusion we are going to get into some code we're definitely going to look at examples as always if you're wondering source code is available through patreon thank you to all of you who support this Channel and really help me keep making these videos so I really appreciate it now let's jump into the code so this is pretty familiar territory we're looking at a simple program that does nothing but print out hello and because we're talking about prototypes let's make a couple of functions okay so let's just make a void one called Foo and let's make another one called bar okay so these are little functions they're not really going to do much let's just have them print something out and we'll say this one prints out Foo and it's from Prince al bar okay it doesn't really matter but so we have this program it has some functions and the question is is why did I put these up above Main and somebody may have already explained this to you but basically uh before you can use a function so let's say down here I want to call Foo and I want to call bar right so if I want to do that and let's just make sure it all compiles which it does but so if I want to use Foo and Bar in main then I already have to have defined Foo and bar right so these need to come before main because otherwise Maine wouldn't know what they are right so that's just a rule and see some languages don't have that rule some languages if you you can Define them sort of anywhere in the file and they'll still be visible to all other functions that's not the case with C and C plus plus with C and C plus plus you have to actually declare a function before you can use it okay so where do prototypes come in well often somebody who's brand new to programming will come in and and they'll see something that looks like this okay so so they'll say oh it looks like a function definition but there is no code now this is something that we call a prototype right and we could make a prototype for bar as well and all point of prototypes is to describe the arguments in this case no arguments and the return type for a function and to give it a name but more it's just that giving the compiler a heads up now the reason that we might do this is because this frees us up once we put the Prototype up here now I can come down here and I can say you know I happen to want maybe yeah some people do this they want Maine to be the first thing and then they put all the functions down at the bottom so someone reading this code comes in and sees this is what the program does and then if you're curious about what food does while you come down here and you check out bar and all these things right and if we didn't have our prototypes here this would give us a problem right this would give us an issue you know we've got some implicit declarations it's like I don't know what these are but if we come in here and if we put the prototypes in place then we compile it and it's fine basically so what we're doing here is saying hey compiler I want to give you the heads up that there's a function called Foo it takes no arguments and it returns nothing and so then I'm fine to use these wherever I want so you'll often see these in header files for libraries and things like that so when you include them basically you don't have access to the code your program doesn't know for example what printf is but if I provide a prototype for it which is provided in standardio.h well then it's just saying hey by the way this is what it looks like and then the Linker later on can link it all up and make it all work but the compiler first and foremost needs to know what this function looks like and how it behaves in terms of like how many arguments it takes in and what it returns and really that's all there is to it folks so if you see something that looks like this don't be intimidated don't worry about it it's just a function prototype and it's just some programmer's way of trying to give the rest of the code the heads up that this function Foo is a thing and it's going to be called and it's all okay so I hope that's helpful to you beginners out there who are just getting started or who knows maybe some of you're a little more intermediate and you never came across function prototypes that's fine too it's a judgment free zone but I hope you learned something new I hope you enjoyed this please do click something on your way out like subscribe tell a friend and until next week happy coding and I'll see you later
Info
Channel: Jacob Sorber
Views: 16,359
Rating: undefined out of 5
Keywords: function prototype c, function prototype c++, function prototype call, function prototype in c, c function prototype, function prototypes in c, function prototype c programming, function prototype cpp, prototype function c, function prototype, function prototype call in c, function definition in c, prototype function definition in c, c programming example, function prototype example
Id: jxW7iwCtBZk
Channel Id: undefined
Length: 4min 38sec (278 seconds)
Published: Tue Jun 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.