All right.
The long awaited moment when we finally dive into functions.
I apologize for the wait for so long, you've probably been like, Oh my gosh, he keeps introducing
all these new quote unquote functions that we're using.
Yet we still haven't really had a specific lecture on what function is and how we could make our own
functions and stuff like that.
Well, no longer.
We now are on that topic.
It's going to probably take a few lectures to cover it because it's a big topic.
I really want to.
Make sure everyone understands because it is a topic that has kind of a steep learning curve, maybe
for certain people to understand what it really is and what happens when that, like our function gets
used in your code, was the computer do.
So we'll have that part where we talk about what's really happening behind the scenes.
And then in this lecture, we're going to kind of go over the basics of what a function is and get into,
like how we code the functions and stuff like that and talk about all the parts.
So let's finally get started with this.
So first off, I'm going to preface it with talking about some programming styles.
So what we've been doing so far is something probably referred to as procedural programming, and this
is something that I'm sure some people on StackOverflow can argue about.
But it kind of is is what we've been doing.
I haven't really mentioned it before, but we're programming right?
And there's a few different types of programming which are kind of like programming styles.
But what we've been doing is making programs that, of course, use control structures that the loops
and the conditional, if else, if and else kind of stuff.
And we've included some logic with brilliance and stuff, and we've been following sort of a top down
approach.
Right.
So we kind of have our main function and we start at the top and then we just do stuff in order, right?
We make some variables at the top.
Then we like do some calculations, maybe we have a loop, maybe we have some conditional statements
and then once we have our results, we kind of like, you see out and we print them to the console.
So we're going to continue on this path of procedural programming for a while, but we're going to introduce
a kind of sub I would I would consider it like a sub style of that, which is modular programming.
And so rather than just like following the top down approach, we're going to do something where we
break the program into different sections.
And this is kind of part of procedural programming you'll see in the definition.
If you look that at it, it does talk about breaking down a routine and do some routines and stuff like
that.
A routine may be considered like the whole program.
The program is like a process and you have some sub processes.
You know, subroutines could be considered functions and stuff like that as well.
And that's what we're going to do.
We're going to introduce this topic of functions where we can kind of break up the work that needs to
be done rather than just only using the main function.
So we've been using functions, yes, and I've been talking about them, things like get line that is
a function pretty much when you see a word that is a key word and it has these parentheses after it.
That is a function call.
So we've been using functions and we've been calling functions, but now it is time to make our own
functions and really try and understand them better.
So.
We've been just using the main function, that's the function that we've been writing ourselves each
time, right?
We've kind of put into Maine and then some parentheses and some curly braces and inside the curly braces.
We've been doing all of our program basically, right?
So let's say that we don't want to have to write everything in Maine.
We want to have some of that stuff in between.
The curly braces in Maine be accomplished elsewhere in the program.
So we want like another version of Maine kind of write another function that can handle some of the
work.
So in this example, you know, let's say Maine says, all right, function one, you do some stuff
function to you do some other stuff that way.
I don't have to handle all the workload.
So what do they really do and what is their purpose?
Well, the purpose of functions is that is to divide up the work your program means to do so.
Just like the main function, we can make some other functions so Maine doesn't have to handle all of
the work.
We would perform some specific tasks with these functions.
That's what the functions would do.
They would be assigned like a specific task to do, and Maine would call that function and be like,
Hey, you do this for me and function.
One would do that task.
So another huge benefit of functions is when it arises, when you need to perform the same type of task
multiple times over and over, but maybe on different data.
You don't want to have to rewrite the same code over and over again when you could just have the function
do that.
And each time you want that stuff done, you could just call the function.
You could hit it up and be like, All right, please do this for me again.
So that's what we're going to see in a moment.
So we should revisit some of the technical aspects of functions.
These are things that I mentioned a while back in another lecture, but I kind of want to refresh and
then dive a little deeper into it.
So one thing we talked about was that functions have return types.
So function is like, I'm going to do stuff in here.
This box is a function.
After it does stuff, then it's going to maybe pop something out, and that is going to be a certain
data type, right?
It's going to do stuff with some data and then put out some data, potentially.
There always is going to be a return type that you must declare for the function, and that can be an
end, a float, a string or something called void, which is basically nothing.
That's just saying like, we don't have a return type, but you still need to like clearly state that
the function is void.
You can't just not put a return type.
You got to put some sort of return type, even if it is like a do nothing return, nothing type.
There's also much more as we'll see as we get further into the course.
We're not limited by these primitive types or even some of the other types that we've discussed, like
string.
We are not limited at all by what we can return from a function, pretty much.
So we'll just see more and more types and we'll well later on talk about creating our own types.
So this is just about you understanding that functions return stuff potentially, but must always not
potentially must always have a defined return type, whether it returns something or not.
You must define a return type and void.
It would be if it does not return anything.
You still must explicitly say this function is void for the return type.
Another thing is that they can receive data so they can have in-flight streaming and more, of course,
because there's other data types we haven't discussed.
That stuff can go into the function, so the box is still the function and stuff comes in and the functions
like I will do stuff with the data that I received.
And it doesn't always have to receive data.
This is like it can potentially receive data if you would like it to.
Unlike the return type, which it may or may not return, it's something, but it must be defined what
the return type is.
OK, so let's let's let's take a look at what defining a function is versus calling a function.
So there's two distinct kind of phases of functions.
One is where you define what the function does.
Right?
You need to actually put a description of what's going on with the function.
So that would be the function definition.
You see the arrow here.
This whole section here is the function definition.
So to circle, it would be like this.
This is the function definition.
All this stuff here.
So down here, this arrow is the function call.
So this is a saying what the function does.
And it's actually putting the code that will be ran when we call the function, and this is us calling
the function because we want the function to do something for us in this very basic example.
What happens is that it's a void function.
It does not return anything, but it does something right.
It prints out hello from my function.
So you see that down here on the screen.
And don't worry, we are going to break down these programs so you understand what's going on here more.
But right now, this slide is just to tell you that there's two distinct parts the definition and where
you call the function.
So let's talk about the definition and break it down.
What is this right here?
Well, let's start at left and right and then go into it.
So right here, this is the return type, and we talk about a void just as this function does not return
anything.
But like I said, we must explicitly define that we must say this function returns nothing.
So that's why we say void.
Just like down here with Maine, we've always been writing and Maine, right?
And why is that?
Because the return type is an end.
Look right here.
It says return zero.
And we've talked about this a little bit, and this is the integer that gets returned to the operating
system after the program finishes.
So just like Maine, we also have to put a return type here, we have said in this example that there
is no return type, so we put void.
Let's move to the right.
This right here is the name of the function.
Yes, we get to name our own functions now.
We do not have to put a specific name like Maine anymore.
Maine is a special name that is known by the programming language, right?
The compiler sees this and it's like, Oh, Maine, I know that this is us defining our own function
name right here, and we're calling it my function.
This is also a slight style guideline example here that we'll talk more about later, but you notice
that it says my and then there's basically is two words.
It's my and function, but we smash them together for the name.
And the second word has starts with a capital letter.
This style is something called camel case, and it's kind of the de facto styling for naming conventions
and for functions, at least in C++.
We'll notice that we can use Camel Case for different things in programming, and we'll talk more about
C++ style and stuff like that.
Different programming languages have different style guidelines, but that's something for later on,
not quite as important as learning how to program in general.
So the blue part is function is the function name.
Then we have some parentheses here.
And these parentheses are empty right now.
But remember, we talked about the fact that if it is possible to have some input data going into the
function, right?
So it can take some data in instead of just returned data out.
It can do both.
It can do, you know, neither or it can do one or the other or something like that.
So.
These things are called parameters, if there's anything inside the parentheses, here it is, the functions
input data where we probably want it to do stuff with that data.
And the name for that is parameters, so multiple things can be inside here.
Or just one thing?
Those are called parameters.
Then in between curly braces, that is the function body, just like the function body of Maine, is
all of this stuff inside the curly braces.
It's just like kind of inside, and if right or inside a loop, there's curly braces and stuff that
happens inside the loops inside the curly braces, right?
That's kind of like the body of the loop.
You could think of this is the body of the function.
All the stuff that happens inside the function.
So those are the four main parts of the definition.
Let's continue on.
So this is the function call.
You've noticed my arrows kind of overlapping.
Sorry about that.
But inside Maine, we are calling our function by using the function name.
So we have my function that is this name of the function, right and the definition and then we have
some empty parentheses and a semicolon after the parentheses are empty because we never defined any
parameters up here, right?
There's nothing inside of these parentheses in the definitions zone.
If there were, then we would also need to put some stuff in the parentheses here.
We did, in fact, not just need to put some stuff, we need to put the exact number of parameters as
we're in the definition.
But down here and we will see what this really looks like in a moment when I give you an example with
parameters.
But when you put the input data in the function, call for it to get sent up to the function definition.
It's called arguements when it's down here.
So definition, we call it parameters for the stuff in between the parentheses or the function call.
We call it arguments.
So they're both function input data you see, right?
But we call it argument somewhere down here in the call.
So let's take a look at that where we have parameters and not only parameters, but rather than having
a void return type, we actually have an integer return type just like main.
So let's go ahead and break this down, so I'm going to talk about the call first because we're going
to act like we're starting out in Maine.
So what's happening is that?
The main function gets called when the program is run, right?
So we have this at numbers right here.
This is our function name.
What we're doing is we are passing the functions, so you can imagine this like some kind of game with
a ball, right?
Imagine that we have, you know, kind of multiple it's like a football or something you can think of
like that, whether you're talking about football, like in the European sense, like soccer or American
football either or.
But let's just say that we have, you know, two footballs right here, and but somehow one person is
going to pass both to this other player, right?
So Maine is like saying, OK, I'm going to pass to I'm going to pass this input data.
Over to this other player.
So we have these arguments here, which is the phone function input data, which is kind of like the
ball that you're passing, right?
The ball has multiple things or you can think of it as two footballs or something like that.
We take it and we send it up here and you notice this is going to go here.
And this is going to go here, that's because they are positional arguments.
All right.
Five is the first thing in the parentheses.
Three is the second thing in the parentheses.
So that means that five will be right here when we pass it, the five gets passed up here to number
one.
It's just like assigning it to a variable like imagine we had and no one down here in Maine and we said,
and no one equals five.
That's kind of what's happening here, except it's happening automatically when we put a five right
here.
The compiler sees this and it's going to add numbers and it says, Oh, OK.
Number one, it contains the value five, because five was an argument, in fact, it was the first
argument, so the first parameter is going to hold the value five.
Notice there's a comma separating both of them, so this is the first one, then we have a comma, then
we have the second parameter and down here, just like the second parameter, we have the second argument,
right?
Three is the second argument.
So just like you might have thought, three is getting saved in two.
So now let's jump up to here and talk about this.
Number one contains five and two contains three because we called this function, add numbers right.
We can now refer to number one and get its value right.
We're allowed to use it because the function has knowledge of the parameters here.
It understands what these parameters are.
It recognizes the names.
So we are allowed to do things inside the body of the function with number one and number two.
We're only doing one thing, though on one line.
And what we're doing is we are returning number one plus numb to.
So you don't necessarily just have to return one single number, like the return of zero here, you
can also return an expression that is an integer as well, right?
So if you have five plus three, that is eight right, five plus three is eight.
Imagine that now what this boils down to is like return eight.
So you see this says returns zero.
We're saying return five plus three, which is number one and number two, that's what their values
are inside of these variables.
So it's essentially the same thing as saying Return eight, which is kind of related to this down here,
right?
Returns zero.
It's kind of a similar thing.
So we return eight and it basically passes the ball back to here where we left off, right?
It's like playing catch.
Mane said Here, let me pass you some data, pass the data up the function, add numbers, say like,
Hey, let me do something with this data, I'm going to add it together and give you something back.
So it added five plus three.
That was eight.
And it says, Hey, hey, yo, man, I'm going to pass eight back to you.
And so Mane catches it right here where it left off.
It passed two things up here.
This added them, and it passed one thing back.
And now just eight is right here.
You've seen something like this before a variable with an assignment operator.
Now imagine there's the number eight right here.
So what happens is that eight gets assigned to this variable now.
So you save the result to a variable.
So that's what happened, what the compiler did first or actually when the program was running, I should
say.
Is that ad numbers got called and it got sent data.
It did some stuff, returned it back to here, and then we did the part where we assigned that result
from the return to this variable called result, right?
It's an integer variable and eight is what we got back and eight got saved into here.
It's equal sign.
It gets saved in here.
Finally, we print that out and you notice that in the console we see the number eight.
So let's walk through this previous example again, just to really break it down, because it's kind
of complicated.
We start here.
I'm going to break it down step by step with the arrows so you can see what's going on rather than me
just dragging my pointer around.
So we start out in the program when the program runs, it goes to Maine, so we go inside of Maine.
Then we see this line here on Line Nine.
It says Entwistle equals add numbers.
Rather than doing this right away, we kind of push that off to the side.
What we do first is we go to the function call because we need to handle this.
So we see, oh, add numbers, we're going to pass it some data.
So we go up here, we pass it, five here and three here.
So the variable of type integer number one contains five.
The variable of type integer known contains three.
We now go inside the add numbers function definition to the body.
We are now in the body of the function.
The body only has one line, it says return number one plus tip number two.
So what we do is we evaluate number one plus number two.
No one plus two is five plus three.
That value is eight.
Now we look at the fact that we must return eight.
So that's what we do.
We return eight and we pass it back to this area right here.
We have now come back to this line.
This area over here has the value eight, which gets now saved with this assignment operator into a
new variable, a new variable integer result, type integer and it now health evaluate.
We then had to line 10 and we noticed that we must see out whatever is inside of the variable result
and then must have an in line.
And that is what we do.
So that prints out to the console.
We see the value eight.
Now we hit the bottom of main and it says return zero back to the operating system and the program finishes.