Parameter Passing Schemes

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 lectures, we explored foundational topics related to user defined functions. We've looked at how to declare functions using prototypes, how to write the code for functions and their definitions, and how to use functions by calling them also known as invoking them. Now that you have the foundational skills, we move on to a slightly more advanced topic. The topic of parameter passing schemes. We will specifically discuss passed by value, which is what we've done so far without really even knowing it and passed by reference, which allows us to give more control to the function over the values of the parameters. Pay close attention to the details of these parameter passing schemes because you have projects at the end of the section that depend on using the appropriate parameter passing scheme. What we've been doing thus far when we pass anything to a function is passed by value. We can think of passed by value as passed by copy. A copy of whatever we place as the argument is made, and then that copy is stored in the parameter to be used inside the function definition. Recall that the word argument is what we call the data placed between the parentheses when we call or invoke the function and that the term parameter refers to the name of the variable used locally in the function definition. Again, passed by value means that the argument's value is copied into the parameter, which is then used locally in the function itself. Let's look at some code and this will help us hopefully make a distinction in just a little bit with passed by reference. Let's create a project called passing schemes. So, first we'll create a new project, empty project. Next, we'll call this passing schemes, create. We will, of course, have our initial file here and then we will add our skeleton code using name space standard and main return zero. Okay. Now, let me see here. We want to fill in or at least give a prototype here. Void value changed one. This is a test we're doing here to see if we're actually changing the value of the argument passed in. And down here, we will of course, have void valueChanged1 int someNum, this is our definition of the function, 100. So, down here it's trivially just changing the value of the parameter to 100. Now, our big question is does it affect the argument that we pass on? So, let's see if that happens. My number, I'm going to say  int myNumber equals 20. And again I'm using integers a lot because they're just simple to work with. You can pass doubles, strings, floats, unsigned integers, long integers, all kinds of stuff. But integers are just very easy to work with. So, okay, before value changed one call my number is we have my number, and then we have we call value changed one passing my number to that. And then we'll say After valueChanged1 call, my number is my number. So, what we're doing is we are printing this out saying before we call this function, what happens? And it should be 20 because we just set it to 20. But then after this function which is sending it to 100, setting the parameter to 100, does that have any impact on the value that we pass in here, this variable that we pass on? Because remember it doesn't really matter if this has the same name as that. In fact, you can pass anything as long as it's an integer variable and it will go in here, store it in this parameter and set it to 100. The question is does it impact the argument of this thing? So, let's see here. Let's run this and see what we get. So, start without debugging. Before it's 20, and then after value changed one call, it's 20 again. So, that's interesting. So, what's happening here? So, it's interesting. We initialize my number to the value 20, and we printed out in main and it remains 20. We call value change one the argument my number is represented by the parameter some num inside of this function. We changed some num in the function. And inside that function, if we were to print it out like this. So, we'll say some num in value changed one is let's do this just to see what happens. So, after that, let's run this. We get 20 for the value that we passed in, that we're going to pass in. And then when it calls value changed, we have 100 because it's printing this local variable, essentially it's a parameter but it behaves like a local variable. Some num in value changed one is some num prints out the 100. But then does it have an impact long term? And the answer is no, it doesn't. It does not change the argument. So, it's very, very interesting. So, the answer lies in the very careful definition of passed by value. The value of my number was copied into the parameter some num while some num lived in value changed one and was changed in there. It's value was actually changed and we can print it out and see the change. But some num is not the same variable as my number. So, some num even though it's representing the value or holding the value of this my number up here, it isn't the same variable. They do not point to or contain refer to the same memory location. They are separate memory locations with a copy of the same value initially. So, how would we write the function that we can actually change it? So, how could we actually change this value of some num and have it impact the my number? Some of you might be thinking we could just make it a value returning function and capture the new value using return statement and that could accomplish our goal but the function itself wouldn't be changing the value of the original variable. And what if we wanted to change more than one variable's value by passing them to a function? So, our solution is to use what's called passed by reference. Let's code a little bit more and we'll see what happens. So, void value changed two, and this time, look I'm using some special syntax, so int and then the 'and' sign. On the standard US keyboard, it is shift seven, that gives you the ampersand sign or the 'and' symbol, and we'll call that some num as well. And then I'm going to actually just to save time, I'll copy that, paste it down here and we will look at this and down here I'm going to set some num equal to 100, just like I'm doing in the one above it and then I'll say inside value changed two function some num is normal print some num kind of like we did with the other one. Wording is a little different but same general idea. And then back up in main, we have to actually try to test this out. So, I'm going to print a couple extra new lines just for some space and then we're going to say my number is currently my number and then value changed two. We pass my number to it, and then print out after value changed two call my number is and then I will say my number endl. All right. So, what the idea is, is that now since it's being passed by reference inside of main, it pretty much looks the same. Right? We're just passing it as an argument to the function. So, if we look here, what's going on or what should happen is this my number is actually being represented by this some num down here and they are actually pointing to the same memory location. That means if we make any changes inside this function, it should have a persistent effect. So, let's run this, see what happens. So, we have the first set of things that we printed out earlier with value changed one. We had the 20, hey, it's changed locally but it didn't persist, and now we have my number is currently 20, right there. The value changed two is called. We set it to 100 and then print it out and say, hey, it's 100. So, nothing different there really from what happened in value changed one but then inside of main, after value changed two call my number which was passed in as the argument is now 100, not 20. Very, very interesting. So, we've made some num a reference variable for this other function. So, instead of getting a copy of the value, the variable actually behind the scenes, so to speak, holds the address of the original variable that we passed in. Therefore anything we do to the parameter inside that function will affect the original argument. You can think of it again as kind of an alias. In our main, the variable goes by the name my number but in the value changed two function to which it is passed by reference, it goes by some num but they both represent the same location in memory. One final note for this lecture is with passed by value, you can pass literals or variables. So, I could have and this one just passed like a 20 or 30 or a 70 just as a literal number. But with passed by reference, you should only pass variables. Obviously, you can't change the value of a literal constant like 150 or something like that. That wouldn't make sense because the whole idea by passing it by reference is to give access to the function to make some sort of modification. Now there's other usages which are called constant references but we are not anywhere near that, it's later in the course. So, before we move on, I have a little challenge for you. You don't have to create a new project unless you just want to. But I'd like you to write a void function named three times N. That takes two parameters. The first parameter should be an input integer passed by value. The second should be an integer passed by reference. The function should take the value of the input integer multiplied by three, store the result in the second parameter that is the integer that was passed by reference and you will have to create another variable to pass it into the function of course from main. After you call this new function in main, print the value of the reference variable that was passed in to verify that it did indeed change the value. So, pause the video and let's see what you come up with. How did that work out for you? Let's take a look at the code together. So, I'm going to add this new function here, threeTimesN and it takes int input by value and then int reference output right there. And I'm going to copy this guy and put it down here and use it as the... Oops, I guess I didn't get the whole thing right. Copy that. And then we're going to paste it down here and use it as the header for this function. All right. So, what do we want to do in here? We want to put output equal to 3 times the input. So, we just say input times three. Pretty easy, right? Now, let's test it out in main. So, we got the earlier stuff up here, we could actually comment this out, remember, control K and then control C. That comments it out, and we're just wanting to test this function. So, int my input num. This one doesn't even have to have a value going in because it's going to be given one. You could give it a zero though. Some people consider it good convention to do that. Three times N, we passed the input of 100 which is a literal in this case but it could be a variable, a function call whatever we want. The second argument has to be a variable. It has to be something we can store something in. And then we print out after threeTimesN, my input num is and then print my input num. So, let's run this and see what happens. We shouldn't get the other output that actually is not necessarily more up there. Let's run this and see what we get. It says there's build errors, so that's not good. My input... oh There we go, sorry. My input num not my input. See, that was an error. It tells me that it was undeclared and we can't use it. There we go. So, now we have what we expect. We have after threeTimesN my input num is 300, which is what you expect because we have the 100 and then my input num what's stored in that down here, threeTimesN, the input is 100. So, it takes 100 times three, that's 300 as a value that it's taken and put into output which is our variable passed by reference and who is represented by output, it's my input num. So, when we print it out, it's now 300. Excellent. So, that's pretty cool but it was rather simple also. So, if you ever want to pass a variable int that the function needs to be able to modify or manipulate, using passed by reference is usually the best way to go. In the next lecture, we will learn about where different variables are accessible. This is a crucial concept. It's called the concept of scope. We'll also talk about lifetime as well, but scope is the region in which a variable can be used in lifetime is how long the variables quote alive or active. So, let's get to it.

 

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