Function Prototypes and Definitions

Function Prototypes and Definitions
Overview
Difficulty
Intermediate
Duration
2h 27m
Students
16
Ratings
5/5
starstarstarstarstar
Description

Functions in C++ are reusable named pieces of code that we can call or invoke when we need them to do something. They help us to take large problems and break them down so that they are more manageable. This course explores functions and puts them to use in a range of projects.

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • College students and anyone studying C++

Prerequisites

To get the most out of this course, you should have a basic understanding of the fundamentals of C++.

Transcript

In this lecture, we will look at how to write our own functions starting with basic function prototypes and function definitions. Functions are, as we will see, named reusable blocks of code that we can call or invoke when we want to use them. Initially, we will see what a program looks like when the entire function definition is placed before the main function. Ultimately, however, we will place the function prototypes above the main function and place the definitions of the functions below main to help make our code more organized and more readable. Let's start by creating a project called FunctionFun1, in which we will write a function definition above main and test it out. So, here we go empty project, FunctionFun1, and create. And of course, we create our new file here, main.cpp, fill in the skeleton, and we could create a function like this. So, void printSomething() and then in here it just says, "Hey! Look I'm here!" and then to actually use the function we call it in main(). All right. So, let's run this, start without debugging. And what happens is main calls this function or says I want to use this function.

It goes to the function definition up here and prints out, "Hey! Look I'm here!" followed by a new line character just as we expected. Even though it's possible to write all of our code like this, it's considered good C++ technique and a good programming convention to have the main function, which is the applications entry point, as the first defined function. In order to accomplish this, we cannot put the full definitions of other functions above main(). But in C++, the names of anything we use must be at least declared in the source code file before we use them. So, we have a bit of a predicament. Can we declare a function before main() but then define the function after main()? The answer is yes. And the way we do this is with function prototypes. So, let's rewrite the code in the FunctionFun1 project to use function prototypes. And then we will discuss everything. So, I'm going to move the definition down here of printSomething(), and then you have to have the prototype which is just basically the header of the function above main(). So, everything in main() just stays the same here.

Briefly, let's see what happens if we comment out the function prototype now and then just try to run main as normal. Says, there's build errors. And I'll say, I know I don't want to continue, says printSomething() identifier not found, that's because even though it's down here, it didn't get a heads up. Main was never told, "Hey, there's this thing called printSomething() and you can call it." So, put this back and then if we run the program now, it works again. So, you should be able to see that the program needs to know the name of the function before it uses it. So, let's get some terminology out of the way. So, we can get a better idea of how to use it correctly. When you just write the functions return type, name, and parameter list but without a body, this is the function prototype, with a semicolon at the end, of course. This is just a declaration of the functions existence really. Function prototypes should go before the definition of the main function. Function prototypes are similar to variable declarations we've seen throughout the course, we have to declare them before we use them.

The same thing goes for functions. When we write the code to describe what the function does like down here, this is called the function definition. Note that the definition has two parts. The first part which matches the prototype is the function header that consists of the return type, in this case, void, the identifier of the function which is printSomething(), and then there's nothing between the parentheses, so that would be the parameter list. The second part is the function body, just like a person that has a head and a body. The function body is the set of matching curly braces and the code defining what the function actually does. Finally, when you write the functions name somewhere inside of another function, such as main, and it doesn't have the return type and you pass it any necessary arguments more on those later that match up with the parameter list. In this case, there are no arguments because there's no parameter, and we'll talk about the distinctions later on. This is called calling or invoking the function. So, main() is calling printSomething().

We would say that main() is the calling function. Note that are simple printSomething() function has a return type of void and an empty parameter list. Even if you have a prototype and a definition, the function doesn't do anything unless you call it. One notable exception is the main function which is automatically called by the C++ runtime system when your program is launched because it is the entry point or where your program starts. So, as a small challenge before we go on, continue using this FunctionFun project. You don't have to create a new one if you don't want to, and just create a prototype and definition for a simple void function called printMyName, in which you will, you guessed it, print your name. Don't forget to call the function in main or you won't be able to see it in action. So, pause the video and try out this little challenge and come back when you're done. How'd that little challenge go for you? Let's do it together. So, we're going to add the function printMyName and call it from main(). So, just above main(). It doesn't really matter if it's above printSomething() or not, but void printMyName(), and then the definition again doesn't really matter if it's before or after this as long as it's before the main definition where it's used. print, void printMyName(), and then in this case, I'm just going to put "John Baugh". And right here, we're going to say, printMyName(), so we have to call it for it to be used. So, let's run it now just to make sure that this works. So, we're getting, "Hey! Look I'm here!" from our printSomething function and we're getting "John Baugh" from the printMyName function. Pretty cool. So, that's it. Great job everyone. In summary, functions have identifier or names just like variables do. And they follow essentially the same rules that variable naming does, and you start them with a lowercase letter typically. Again, the identifiers must be declared before we refer to them or use them. In our case, before we call or invoke the function. So, that's all we need as far as scratching the surface of the function terminology and techniques. In the next lecture, we will dive deeper and discuss varieties of functions with different return types and parameters. Let's get going.

About the Author
Students
316
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics