The course is part of this learning path
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.
- Beginner coders, new to C++
- Developers looking to upskill by adding C++ to their CV
- College students and anyone studying C++
To get the most out of this course, you should have a basic understanding of the fundamentals of C++.
In the last lecture, we got our feet web with the very basics of writing our own functions and learned how to write function prototypes and definitions as well as how to call or invoke functions. In this lecture, we will delve a little deeper into user defined functions by learning about return types and parameters. We will also discuss the difference between parameters and arguments. You will have a great opportunity to demonstrate your knowledge of both return types and parameters in the projects at the end of this section. Please pay very close attention to the topics in this lecture. Essentially all of the projects in this section and much of the code you'll write and post whenever you use it in the future will greatly depend on your ability to correctly write functions with the correct return types and parameter lists to accomplish your goal. First let's create a project called ReturnTypeParameterFun. So, create a new project and then we will have ReturnTypeParameterFun. There we go. We will of course create our little file here, add new item, and we will put main.cpp. We have include iostream... sorry about that, using namespace standard. We have our main function as usual and I'm going to put the prototypes up here for four different functions that we're going to write. Print hello, void print number. Oops, that doesn't look so good does it? Int a, int give me 10 and then int addThese, num 1, num 2. So far we have four function prototypes above main. We'll discuss what void means in just a bit, but note that there is virtually uncountable assortment of function headers that we could produce. I have included in our sample project a simple demonstration of four major permutations of characteristics of the functions based on their return types and parameter lists, namely print hello, has a void return type and no parameters. Print number has a void return type and a single parameter, integer parameter here, give me 10 has an integer value return type with no parameters and addThese has an integer value return type with two integer parameters. So, we call this first one right here, printHello, a void parameter less function because I'm describing it based on its return type and the parameters. So, it's a void parameter less function. The second one is a void parameter raised function because it has at least one parameter. The third one here is a value returning function specifically it's an integer in this case but you could use int, string, double, whatever you need to do to solve your problem. Int means that we have a value returning function with no parameters and then the next one is also a value returning function with two integer parameters in its parameter list. So, we would say that the ones with parameters are parameterized and then the ones without a void return type are value returning. So, let's fill in the definitions and then also some of what main should do. So, I'll do void printHello, and we'll do that as the first one and then we'll come back and write in some more code. So, we'll put Hello there, this one is very simple. And then I'm going to do void printNumber. This one takes a parameter. We've named a, it's not a particularly good name for a variable and I know I keep ragging on you all for you know, you have to use good variable names. But in this case it's very simple. It doesn't have any meaning and it's just to demonstrate a particular syntax. So, right here the number is a. All right. Oops, next one is a value returning. give me, there we go, 10 live television and we have int addThese int num 1 int num 2, return num 1 plus num 2. Now, again an alternative I could have done is I could have stored a result here, like result, num 1 plus num 2. And then just return result. That would be another possibility. So this is just as valid. But a lot of times as you become more advanced, some people prefer this syntax and depending on how complicated it is, this might be better. But since this is so simple, it's just the two values added. In fact in practice you wouldn't do this because you have the addition operator you can use without the function. This is again just to demonstrate syntax and concepts of course. So we're going to swing back around and we're going to look at main here and actually use call the functions. Int num 1 equals 120, int num 2 equals we'll say 580. I'm going to do printHello. And then I'm going to do the printNumber and this one all pass in just a literal, so it's a literal constant right there. I'm going to say int someData equals giveMe10. So, that's going to return the value 10. Obviously it's a very trivial function. So, we have cout someData endl and then we have int totalValue equals addThese, num1 and num2, and then print out the total value. All right, looking pretty good. So, let's run this program and discuss the output briefly. So, I've got Debug, start without debugging. Make sure we haven't made any syntax errors. It ran so I'm assuming not. And so the first time, the first thing that we call is printHello. So, that says hello there. PrintNumber, you'll notice it takes its input the integer and then says the number is with a space and then whatever number you passed in. In this case, the argument we passed in is 100. So, the parameter down here a, gets the value 100, so it says the number is one 100. The next one is a value returning function called GiveMe10. So, when the function gets called, it actually replaces its call, or you can think of it as replacing its call with the value that it returns. So, in some data equals give me 10. It'll grab the value 10 and then we print it out right here. So, it prints out 10. And then totalValue right here, this one captures the value returned by addThese. So, inside that function, we just have return num1 plus num2 and num1 is 120, num2 is 580 so that equals 700, no shock there. Now let's discuss return types in more detail. The void functions that we wrote, printHello and printNumber right here perform a particular test but they don't give a value back to the calling function or the caller, the main function. In other words when you call them, when you use them, you're just telling them to do their job and then they're done. They don't leave any special presents for you where the function call was. Now, that sounds a bit strange until we talk about value returning functions. With value returning functions as opposed to void functions like giveMe10 and addThese, so right here giveMe10, addThese, when the function is called, it returns a value. In our case, both functions return integers. But essentially, any data type is possible. Notice how we treat them differently when we called them. Although we could just call them on their own lines and not capture the value they return, that's kind of a waste. Most of the time, we want to capture the return value. Value returning functions usually perform a calculation or task like the ones we have here performing some sort of task. This one was very trivial but it returns a value and this one does do a little calculation and returns the value of that. So, most of the time we do want to capture that special present that the function leaves when we make the call. So, in our examples, we capture the values with integer variables but you could use them in most places that you would use any integer at all. So, in addition to return type, let's talk about parameters. Well, return types essentially tell us what kind of output to expect with value returning functions or no output at all meaning output from the function, I'm not talking to the console but coming from the function, no output with void functions and output with value returning. Parameters on the other hand, tell us what kind of input our functions need in order to do their jobs. The functions printHello and giveMe10 don't need additional data in order to do their jobs. You'll notice printHello doesn't have a parameter, giveMe10 doesn't have a parameter even though their return types are quite different. It only has to do with their parameter lists. So, printNumber though takes a single parameter that it needs to do its job, it prints specifically that number, and also addThese takes two parameters, adds them together and then returns the integer. So, to do its job, printNumber only needs one parameter and then addThese takes two. There's some more important terminology that I want to make sure you know how to use correctly. It's a fact that a lot of software engineers, even those who are very good at their jobs frequently misuse terms or don't know the appropriate terms for different programming constructs. You might think that isn't important, but knowing the correct name for a different programming constructs makes you better able to communicate with other developers in a less ambiguous fashion. And also helps you so that you know what to search when you're looking up details on something using a search engine. So, a distinction I'd like to make is between arguments and parameters. The arguments are the actual values passed in when you call or invoke the function. Whether they are variables, literal, function, calls or something else, that part doesn't really matter. When you're calling the function, the data you pass to the function is called the argument. So, in this case, there are no arguments passed to printHello. There is one single argument passed to printNumber that just happens to be an integer literal in this case. No argument passed to give me 10, and two arguments passed to addThese. So, it doesn't matter whether it's a literal, a variable, or even if it was another function call that returned an integer, we could put it there because this expects an integer and an integer. So, when you define the function, those are called parameters. That's the contrary. When you define the function, these are actually called parameters. So, a is a parameter and then these two variables, right here, are parameters. So, with the definition, they're called parameters. With the call or invocation, they are called arguments. They actually act, the parameters, like local variables to the function, which we'll discuss later in the section when we talk about variable scope in an upcoming lecture. You can think of parameters as placeholders that receive the value or the argument passed in. In other words, the parameters will take on the value of the arguments that are passed into the function. So, let's make a quick little update to the ReturnTypeParameterFun program to demonstrate something I mentioned earlier. That you can use a value returning function like the integer returning function addThese anywhere. We would use a normal integer literal. So, this means, we can pass one function call that returns a value to another function that takes that value as a parameter. So, let's add something here. I'm going to below all this fun stuff, put the printNumber. And we know that printNumber requires an integer parameter, but it doesn't have to be a variable or a literal. It could be just some other function call that returns an integer. In this case, we know that addThese will add (num1, 50));. So, I'm just going to show you we can mix up the parameters, the argument, specifically in this case. So, I have a variable and a literal passed to this function. This function gets called first, returns a value namely, the sum of these two values, and then printNumber takes that sum, goes into its body and prints the number. Pretty cool. So, we've got 120 and 50 and that should make 170. So, let's run this and see what happens. So, Start Without Debugging. We should get a little bit of a modification. There we go. The number is 170 and that's exactly what we predicted. So, that's pretty cool. This little addition we've made is actually very sophisticated. I wanted to demonstrate a couple of things. First, we look at the call to addThese, that is inside the parentheses of printNumber. Note that addThese takes num1 and 50 as its second. So, addThese by its parameter list needs two integer parameters. When you call the function, it can be anything that resolves to an integer value. So, that's why it works when there's two variables, when there's a variable and literal constant, or when there's even two literal constants or another function call. Secondly, look at what printNumber is doing. PrintNumber again expects an integer as its argument. So, how is it that we can get away with passing a function call instead? And the answer is easy. Since addThese returns an integer value, which I refer to as leaving a special present where its call was. And printNumber expects an integer to be passed in. This is a match made in heaven. It works beautifully. So, before we move on, you guessed it. I have a little challenge for you. I want you to add a function to our ReturnTypeParameterFun called square that returns an integer, takes a single integer parameter, and make sure to create a function prototype as well as definition and call that function. So, it will square the single integer that you pass in, we'll find the square of it. So, it's the value multiplied by itself, right? Pause the video and come back when you're done or if you need some help. So, how'd that go for you? Did that work out? I hope so. So, let's write the code together. I will add this additional int square ( int n);. So, there's the prototype. And again the order doesn't really matter down here but I'm trying to keep it fairly organized int n, we could call it num, we could call it x, it doesn't really matter, but we've got (int n);. And then we're going to return n * n; that's the square. That's exactly what we want. Now, this won't do anything unless what? Unless we call the function. So, let's see what we get. Now, I'm going to do something else here. I'm going to actually call it in line here. Instead of printing it by grabbing a variable first and then printing the variable, I'm just going to call it right here. And that's okay because cout knows how to handle integers. Square returns an integer. So, that's totally fine. It would have been cool if you stored it in a variable, though that's fine too. So, at the very end, it prints out 25 and that's exactly what we expect, because the square of five is 5 * 5, that's 25. Excellent. So, in this lecture, we've discussed some options for writing functions with variations on their return types and their parameters. We could have all sorts of fun permutations of functions with parameters and without which have return values and don't, i.e. void functions. And you will see a lot more in this course, ton of variety. In the next lecture, we will discuss more about parameter passing including some special syntax that allows us to make changes the variables we pass in as arguments. Let's get going.
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.