Start course
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 the previous lecture, we discussed how variables with different scopes are treated throughout a program and how this depends heavily on where the variable is declared. We also discussed the lifetime of global local and static local variables which determines how long a variable exists in memory. In this lecture, we will take our knowledge of functions even further and learn about function overloading which is the process of creating functions with the same name but different function signatures which includes the number, type, and order of these functions parameters. This is a feature that is available and used heavily in C++ but is not available in C++'s predecessor the C programming language. Let's create a project called FunctionOverloading. So, Create a new project, Empty Project, FunctionOverloading. Very good. And we will create our source file, main.cpp and fill in the skeleton. And one thing you'll notice is I don't copy and paste from previous projects. You could do that if you wanted to but I think it helps learn the more you type it becomes part of your muscle memory and helps you learn better if you type it out yourself. Include string, we're going to use string. I'm going to have a couple of different function overloads here, and we'll talk about these in just a moment. So, int getResult, that's num2 and then string getResult (string str1, string str2) and then I'm going to of course, give these bodies definitions after main, okay? Right there, and right here, and we're going to call them after we're done with this. But for results, I'm going to say return num1 * num2. And then for the getResult for with the strings, I'm going to say, return str1 concatenated with a space concatenated with str 2. And finally, we will call the functions here. So, I'll say, int result num = getResult(30, 20) and then string nameResult = getResults same name but different types of parameters, John and Baugh. And then I'll say result num is resultNum, then name the result is nameResult, okay? Very good. And let's run it because we want to see what it does and if it works, no errors compiled ran. So, it says result number is 600 which is what we expect because it's the first 30 * 20, so that's 600. And then the string version name result is John 'Space' Baugh. We passed in John and Baugh as two separate strings. It concatenate them with a space in between them and return to that as the result. So, that's what we expected. As you can see both functions have the exact same name but the parameters are really what helps to distinguish between them. One of them returns the product of two integers and the other returns a concatenated string with the space in between. The identifier of the function and the function parameters make up what is called the signature of the function. Note that the return type is not part of the function signature. The signature is used to distinguish between different overloads. So, you cannot have two functions with the same name, same parameters  and just try to vary the return type, because when you call it that would be ambiguous. When you call them that's how it determines the difference between which one did you intend. In our example we have the same number of parameters for each of the functions, but we have different types for those parameters. One of the getResult functions takes two integers and the other takes two strings. If those are different, we're in good shape. We could create a getResult that takes an integer as its first parameter and a string as its second parameter, and that would make a third overload of the function. We could create one with a string as its first parameter and then an integer as its second, and that would be a fourth overload of the function and so on. So, the order can result in another overload also. So, you're not restricted to just integer and string and then I've done, you can switch the order of them, and then as long as it can tell the difference between them, we're good. We could also create one with a single parameter or even more parameters than the two that we have here or even many different types of parameters and these would also be overloads. As a challenge, I would like you to use this current FunctionOverloading project, don't create a new one. But add another overload of getResult function to take a single integer parameter and to return the cube of that parameter. Recall that the cube is the third power of a base value, that is the base value multiplied by itself and multiplied by itself again. Make sure to test this by calling the new overload in your main function. So, pause the video and see if you can solve this problem. Come back when you're done or if you get stuck and would like to see how I solved the problem. So, how did that go for you? I hope you were able to solve that one. Let's do it together to see how you might have gone about doing it. So, I'm going to add another overload here: getResult, and it's going to take a single integer. And below the other two definitions, I'm going to add getResult again; notice same name. And then we're going to return the cube. This one's pretty easy. We could use the math library which we'll talk about later in the course but this is totally fine and it's for demonstration purposes, so get num * num * num, that returns the cube. And then we want to of course, test it in main. So, I want to say, cout cube result is cubeResult, and oops, I got ahead of myself. We have to actually call this, so cuteResult = getResult, and then we're going to pass 5 to it, and let's see what we get. So, if we run this Start Without Debugging, and we get what we expect. We have the 600 from the original, the result num is, then the concatenation, and then we have the cube which is 125. Awesome. We got the cube value when we passed it in as we expected. And inside of main, notice that we now have three different calls near the top. Because of the differences in arguments passed in, the program knows which definition we intend on calling each time. And that definition is matched up with the function call that's actually called function binding. In the next lecture, we'll look at some of the functions that C++ makes available for us for doing math. I'll see you there.

 

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