Welcome back, everyone, to this lecture on functions.
Now, often as you're coding of Python, you're going to have some code that you actually want to use
multiple times, but you don't always have to want to rewrite that exact same block of code over and
over again, especially across multiple Python files.
It would be nice if you could set it up in a block, they can then just call to execute.
And that's where the function comes into play.
It allows you to just be able to repeat the use of code.
Now, creating a function requires a very specific syntax, including the IDF or defining keyword.
Then you have the correct indentation to concern yourself about as well as the proper structure.
So I want to give you a quick overview of the Python function, structure and syntax.
Before we jump to the editor to start coding.
So it all begins with the death keyword, and this keyword essentially tells Python that you're beginning
to write a function.
And then you have the name of the function you should notice.
It follows snake casing and you can decide on the function name.
Snake casing just means it's all lowercase of underscores between the words.
This is by convention.
You don't have to do it this way, but it is the proper way to do it, so to speak.
Then you're going to have some parentheses.
Later on, we're going to discover we can actually pass in arguments or parameters into the function
using these parentheses.
Then you have the colon, which indicates an upcoming indented block, and everything indented is then
inside the function that's going to be executed each time you just call the name of the function.
Now there's the option of providing a documentation string, which will explain the function if somebody
passes in the name of your function into the built in help function with Python.
We should also note that everything inside the function is indented, including in this documentation
straight.
And then you're just going to have code that you want to actually execute each time you call the function.
Here we have a very, very simple function that is simply going to print hello each time you call.
Name a function.
So, for example, if I were to call name a function open and close parentheses, I would just see the
print hello as the result.
OK.
We should also note that functions could actually accept arguments to be passed by the user.
So notice now what's changed?
I have a name of the function, but now it's going to actually be able to accept an argument and this
argument is a variable called name.
And then what's gonna happen is it's going to take that name and then tack it on to hello.
So if I call name a function, Jose, they'll report back or print back.
Hello, Jose.
So functions can accept arguments to be passed by the user.
So note again, name is being passed in and then we're saying print hello name.
So in the output input, I can see.
Name a function, Jose.
Print out.
Hello, Jose.
Typically, however, we're not just printing results with functions, we typically use the return keyword
to send back the result of the function instead of just printing it out.
Return allows us to assign the output of the function to a new variable.
A question I get a lot from students beginning to learn.
Python is what's the difference between printing in a function and returning in that function?
And again, return allows us to save the results if we just print the results.
We do see the output, but I don't get to save them to a variable to then later use.
And as a quick note, we will have a deeper discussion of the return cured later on in the actual editor
because it's easier to explain when we're actually coding this out.
But as a quick example of what this would look like here have a function called ad function, and it
takes in two arguments.
Number one and number two and then it returns the result number one plus number two.
So the return actually allows us to save the result to a variable.
Notice now how I can say result is equal to add a function of one or two.
And then if I want it to later on, I could print that result.
So most functions will use return.
We are very rarely just using a function to call a print statement.
It depends on the use case, of course, but pretty much we're always going to add a return statement
to a function to actually return a result for use later.
OK, let's start creating functions with Python and the jump to the editor.
All right, so let's begin by setting up our first function.
This function is going to be super simple.
It's actually just going to print something later on.
We'll talk about return.
So to start a function, we say DPF.
And notice we already know it's a function based off the syntax highlighting.
And I'm going to call this function simply say Underscore hello.
Open close parentheses colon enter.
The indentation starts.
And then every time I call, say hello, I can just print out something like hello and let's.
In fact, let's print out another line that says another hello.
So typically, if I wanted to print hello and print another hello, each time I wanted to print both
of these, I'd have to copy and paste both lines.
But now that I have a function, what I can do is simply call, say hello and I execute that function
with open and close parentheses.
Pay very close attention to my indentation here.
Say hello is no longer inside the function.
And what's nice about the code editor of this visual studio?
When you define the function, you can actually collapse the function by hitting this little dropdown
arrow key.
And then you can really see what's inside your function that actually helps you kind of read your code
and save a little space.
If you want to collapse the function code, so again, be very careful, don't accidentally do something
like this.
In which case you're calling, say hello within, say hello and you can check for doing that by collapsing.
So again, pull say hello out, which means I'm actually going to execute the block of code inside.
Say hello.
So when I run this, I see hello and another hello, and I can call this function now as many times
as I want, so I can call it twice here.
Say hello.
And so we should see it.
Hello, another hello.
Hello another.
Hello.
OK, now I didn't mention typically, you're not actually calling print statements like this within
a function.
Instead, you like to perform some sort of logic or calculation and then actually return the results.
So let's talk about this return keyword.
Return allows you to actually return something to be saved as a variable.
So I'm actually now just going to return the string.
Hello.
And you're going to see what happens when I call say hello.
So notice now I'm just returning it.
I'm no longer printing it.
And when you run the Python code now after saving those changes, you actually don't see anything output
here.
And that's because return is just saying, Hey, I'm going to give you this string.
It's up to you to do whatever you want with it.
So, for example, I could save it to a variable like this.
I could say my variable is now equal to say hello and then I could print out.
My variable save those changes, and now you see hello, print it out, what you cannot do is something
like this.
If I were to say.
Prince, hello.
And then try to do an assignment like this online for this actually won't work, you can't really assign
print hello to this and then print out that, well, you're going to end up seeing is hello and then
you're going to get this weird nun.
You get hello because you did call say hello, but then say hello is not actually returning anything,
which is why my variable then becomes not.
It's just a placeholder there.
So typically, we want to be able to save the results of all the hard work we do within our functions,
which is why we end up returning things within the functions in order to save them as variables.
You may or may not want to print out that variable, but returning it allows you the chance to actually
save it.
So let's start adding in some complexity by allowing for arguments.
So now I'm going to say hello to someone's name, so I'm going to be able to accept an argument name
and I can accept as many arguments as you want.
And you can also call this variable whatever you want this argument parameter, you can call it whatever
you want.
And now what I'm going to say is return the string and it's going to be an f string literal this time
that says hello, curly braces and then the name that's passed them by the user.
All the arguments up here are going to be passed in by the user.
So now I'm going to say my variable is equal to the result returned by say hello, which is going to
be this string literal.
It says hello name.
If you just run it like this, you're actually going to get an error because it expects a name to be
passed it.
So if I run this right now, it's going to say, Hey, say hello is missing a required positional argument
name.
So how do I pass in the argument?
I say name is equal to and then whatever you actually want to pass in as the name.
So now when you're running this code, you see, Hello, Jose.
The user is passing in.
I want name equal to Jose.
And so it's executing Say hello, it's returning back the string.
Hello, Jose, save it to the variable and print that out.
And we can pass in more than just one parameter here, I can say, taken a first name.
Karma taking a last name, and then I could return something like, hello.
First name.
Space.
And then insert last name, so say hello, and then now I have to make sure these match up, you can
do it by position or by name.
So I could just say something like Jose Comma Porsche here.
Save that.
And then when you run this code, you should see.
Hello, Jose Padilla.
I'm doing it by passing in by position, but it's often better just to be really explicit and say,
OK, first name is equal to Jose.
Last name.
Is equal to Porsche that way, whoever's reading your code later on.
Maybe you have one hundred lines in between this function call and the sexual assignment, in which
case it'd be nice to know what these variables or arguments actually stood for.
So let's start adding in a little bit of logic.
In fact, let's make a function that is going to check if your a number is less than or equal to 100.
So I'm going to create a function the f called checker and this simply text if your number is greater
than or equal to 100.
So I take in the number and I'm going to say some comparison logic.
If the number.
Is greater than 100.
I'm going to return greater than 100.
Else I'm going to return.
Not greater than one hundred.
The air there, so I have that function checker.
Go ahead and save those changes and then what I can do here is print out the results of checker and
let's check 90.
And again, I'm printing the results so I can see them here in the output.
And 90 is not greater than 100.
Perfect.
So it looks like it's working as expected, let's put in nine hundred there.
Save that.
And then it looks like nine hundred is greater than 100.
Perfect.
And keep in mind here, we're just showing very simple examples, but we can actually get quite complex
of this.
Let's imagine that I have a list like my list and it's filled in.
If a bunch of random numbers, I'm going to pass in a bunch of random numbers here.
We can think of this as maybe a data stream that's coming in, and I actually need to know whether or
not the number 10 is in that list.
So how can I do that?
How can I check if 10 is in that list?
One way I could do it is by actually using a for loop to go through this list.
I should note there's a better way of doing that.
You could just use the in operator, but I'm going to pretend I don't know that for right now, just
to get the idea of logic within the function.
So I'm going to have another function here that's called check or checker again, and it's going to
take in the list to check.
So let's go ahead and just pass in.
List to check.
And then what it's going to do is it's going to say for number in the list to check.
Note that has to match the input variable.
So for every number enlisted, check.
If.
That number is equal to, let's say, 10 is what we wanted to check for.
We go ahead and return true.
So what's going to happen here just for this first part of the logic is I'm going to go through every
number in this list here and then I'm going to say, OK, is that number equal to 10?
And if at any point this happens to be equal to true, the return hits and then I'm done.
You'll notice that once you hit return in a function, the function ceases to operate.
Now let's say I go through this entire for loop and this never actually returns.
What do I want to return?
Then I want to return false.
So a question is where should I put the return false?
Should I do it here?
Return false?
Or should I do it here?
The indentation can sometimes get confusing.
It should go here in line with the four.
And why is that the case?
Well, because I want to make sure I go through all these numbers first and then if I don't happen to
return true at the very end, I'll return false.
A really common beginner mistake to try to solve this sort of problem would to be use an else statement
here.
And then they'll put return false inside this else statement.
But the reason this is incorrect is because at the very first number at one, what's going to happen
is, well, that's not equal to 10.
So I'm just simply going to return false.
It will immediately stop at the first number.
Instead of having the chance to look through all of them since we want it to have the chance to complete
that for loop, we don't end up using that statement and we line up the return over here with that for
a loop.
So just try to keep that thing in mind.
It's really helpful to sometimes use prints to decide what's actually happening within your block of
code.
But let's go ahead and use checker here.
I'm going to call prints the result of checker on my list.
Note my list does have a 10 on it, so I would expect it to return true here.
And we're going to say Python Example Pi, and let's expand this.
So Python example pie and it looks like a return, true.
I'm going to zoom out one level here swing series thing that's going on.
OK, so let's remove 10 now.
So there is no longer a 10 on that list.
We'll go ahead and save that change.
Let's go ahead and run Python.
Example Python.
And now we get false.
So you can see us beginning to combine many, many different ideas here.
I'm combining the idea of a function, the idea of Boolean, the idea of a for loop, the idea of if
but statements of comparison operators.
It's all coming together to build some sort of functionality.
What we're going to do next is give you a series of problems where you have to write a function that
returns something and they're going to get increasingly more complex and more difficult just to make
sure you understand the building blocks creating functions to end this section of the course.
OK.
Thanks.
And I'll see you at the next lecture.