COMP1010 Function Overloading and Scope

COMP1010 Function Overloading and Scope


Hi! This is COMP 1010 at the University
of Manitoba. I’m Dr. Celine Latulipe, and in this video
I’ll be talking about functions in Processing and how functions can be
overloaded, and also going over scope of variables within functions. So one thing
to note is that you can nest function calls inside one another and any
function can call any other function as often as needed. So it would be perfectly
normal, for example, to have functionA which inside of it calls functionB and
have functionB which inside of it calls functionC and functionC could do some
stuff and might have its own other built in function calls like a println()
statement and so this is totally okay. So function overloading is the idea that
you can have multiple functions that have the same name and this is a way to
be flexible, to create a whole set of the same functions that take
different parameters. All of these functions would do basically the same
type of thing, but it’s just more flexible because they can take different
types of information, to do that thing. So they each have to have a different
signature, which is the number and types of parameters. So if you look at the
absolute value function that’s built into Processing, you won’t find one
function, you’ll find four. There’s the version that takes a double
and returns a double, this is a version that takes a float, returns a float, etc.
And so this provides a lot more flexibility. So here’s another example:
Processing’s fill functions, which you’ve used already and you’ve used them in
different ways. So there’s a version of fill that takes
one int and that specifies a grayscale. So if you want to fill with
black you do fill(0). There’s a version of that that also takes the alpha value for
transparency, so if you pass in two numbers, Processing is going to treat it
as a grayscale value and an alpha value. If you pass in just one
Processing knows that that means you you’re just passing in a grayscale
number, if you do a version of fill where you pass in three numbers it’s going to
treat these as int RGB values, and if you pass in four numbers to fill, it’s going to treat it as RGB values with a transparency value added on at the end.
And there’s actually a couple more because there’s versions of fill that
take the hexadecimal color codes. So the signatures of these are different, so
we’ve got fill(int), we’ve got fill(int, float), we’ve got fill(int, int, int) and we’ve got fill(int, int, int, float) So these
signatures being unique is how the compiler knows when you call fill, which
one to use. So we also want to pay attention to scope and we’ve already
talked about the fact that local variables inside of a function are only
in scope within that function. Now we want to differentiate between the fact
that there’s local variables that you explicitly create, and the local
variables that are the parameters that store the parameter data that’s passed
in. So parameter data that’s passed in to a function, those are local variables
that are automatically created by the compiler (you don’t have to create them
they just are there) they’re in scope through the *entire* function. Local
variables that you explicitly create in a function are in scope from the point
where you created them to the end of the function. So if you create them right at
the top, they’ll be in scope through the whole function, but if halfway through
your function code, you create a new local variable, it’s only in scope from
there to the bottom of that function. Any variable that has the same name
that’s outside of the function is a different variable – it’s a different
piece of memory and you won’t be able to access it, because you’ve got a local
variable of that name. So let’s go through an example. Here’s some code
that’s functionOne in here, we’re creating two local
variables that we explicitly declare, they’re not parameters. So we have x
that’s storing 3, and we have y that is going to store the return value of
calling this function. So functionTwo is down here, it has a parameter. So there’s
a piece of storage that’s automatically created for y and it stores whatever
data is passed in, so in this case passed in three. So y would be storing
the value 3 and so x equals y which is 3 plus 2, x is going to be 5. So let’s just
walk through this and see what it looks like. What’s important to note is that
the red and blue X’s and Y’s are different. So when we first come in here
x becomes 3, y doesn’t have a value yet, until we call functionTwo and get a
return value. So we come down to functionTwo, we’ve passed in what was in x, we
passed in a copy of that, so 3. So y becomes 3, the next line we’re going to
do a calculation, we’re going to take 3 and add 2 to it, so in x which is our
local x (this is different) we’re going to store the value 5. Now we’re doing this
statement and we’re actually assigning a value over top of our local
parameter variable, we’re assigning 0 to y. So even though 3 was passed in, we’re
now overwriting it and putting the value 0 in it. And then we’re going to
return x, and so x (which is 5) gets passed up to here and stored in y. So it’s
important to note that when that y value was changed here, it did not change the
value that that was copied from the 3 that’s up here. Okay
so the important thing, the reason that scope is so useful, is that it gives
you independence. So if you’re a programmer and you’re writing a function,
you can use whatever parameter names you like. And you can create any local
variables and give them any names you like and use them and then some other
program or somewhere else can use those names in other functions and it doesn’t
matter because the variable names are local to the function. You
don’t have to worry about clashes when you’re using those local variables.
That’s why it’s important to try to use local variables whenever possible and
avoid using global variables unless it’s absolutely necessary.
The only thing that you have to be careful of when you’re writing a
function is that if there are global variables that you
need access to (say there’s a global variable called
boxHeight), you don’t want to create a new local variable called boxHeight
because then you will not have access to the global variable.
You’ll override those and hide the original meaning that’s in that global
variable. So let’s go through one example of scope with globals and local
variables in functions. So here we have some code and we’re going to walk through
it. We’re going to see what gets printed out in the console and we’re going to look at
how these variables are created and when they exist. So when we first start, we
create a global variable called x. It’s an int variable, it stores the value 56.
And then we go into our setup() function and in our setup() function we have this noloop statement, and what that does is it just means that draw() will only be
executed once, it won’t be executed over and over again. We print out what’s in x
so that prints out 56 to the console. Then we create a new local variable
that’s also called x, we store 89 in it, so now we have two
versions of an X variable: one that’s a global and one that’s a local. And when
we do the next println(x) call it’s going to take the local variable and it
will then print out 89. Now we’re done setup() and so that local variable just
disappears – it ceases to exist. Now we’re going to do our draw() function. We create another local variable called x. This time it’ss actually a float type. We store
78.23 in it. We go to print and that will print this local version of
the variable, because once we’ve got this local version called x, we no longer have
access to the global one. Now we’re going to actually call this other() function and
so when we call the other function we’re going to print x. In this function we
don’t have a local x, so we’re going to print the value of the global x, so
that’s why we print out 56. And when this function is done, we return back up to
draw() and then draw() is done and that local variable ceases to exist. So
hopefully that helps you understand the difference between these global
variables, which exist the entire time the program is running,
and these local variables and how if you have both a local variable and a global
variable, then you will only be able to use the local variable (if they both have
the same name). Okay that’s it! Thanks for watching!

Leave a Reply

Your email address will not be published. Required fields are marked *