Let's get into user-defined functions. Now, every programming language has functions, however they are going to look different depending on the language. If you have any experience with PHP or JavaScript, functions in Python are going to look similar. So, we are now past the need to use terminal, and from here on out we are going to use our IDE. In addition to functions, we are going to cover a few of the PEP guidelines. PEP is basically a style guide for the Python language, and the first thing we need to do is drop down two lines from the top of the script. To begin defining a function in Python, all you need to do is type the word "def". When naming functions we must be mindful of the PEP guidelines. With Python you will be using snake case, words separated with underscores, when naming your function.
def my_function():
Within the parenthesis we can pass parameters into our function, then we end the line with a colon. So, now we have a function but it doesn't perform any actions, yet.
def my_function(): print("This is my function!") print("A second string.")
my_function()
Now our function will print out two strings. Notice that our IDE automatically indents 4 spaces. When you indent in Python you are creating block of code. When we skip a line and remove the indentation, we are telling Python that our function is completed. Lastly, we will call our function. In the next video we will talk about arguments and how use them inside your function.
Alright guys, so let's get into functions. Now every programming language
basically has functions, I mean they're going to be different depending on which
programming language we're discussing, even cobol has functions but they're
radically different because that's just an entirely different language. However,
basically if you have any experience with PHP, JavaScript, functions are going
to look a lot,
very well very similar. So, we are now past the need to use an interpreter and
what we're going to be doing from here on out is using our IDE that we have
installed. Again I want you guys to follow along maybe even do some extra
repetitions of this just to get used to it.
So, the first lesson here in the subsection of functions is going to be
building a function, so pretty simple, we're going to build a function,
we're going to call it, and we're going to run the script. So let's go ahead and drop
down a couple lines, and also we're gonna go over some pep guidelines
throughout this course. We're probably going to have a module
specifically for pep, but as I go along
and I see or think of anything that's addressed in the pep guidelines,
i'll let you know. So basically what pep is is Python's style guide, so it's like
how to write certain things and certain you know we always drop down two lines
between any texts or at the top of the script. So, we would have our imports up
there, and then drop down two lines, and then start coding. So we drop down two
lines we're gonna instead of typing function we're gonna "def" and what
this is telling Python is that it's going to define a function. So we
write def and then the name of the function. Now regarding naming functions
there's a specific way to do it according to the pep guidelines.
This isn't new, this specific instance, it's using snake case for function names.
So if you know camel case that's basically like this that's that
looks like, the first letter of each word is capitalized, that's used in
Python for class names but it's not used for functions. When you define a
function in Python you're encouraged to use snake case which is separating words
with underscores, so that's what we're going to do. So i'm going to create a
function here called my function. So basically so far we've got "def" which
says we're about to define a function, we've got the name of our function, and
then we've got brackets right here and these brackets are going to be used for
parameters which we're going to be going over in one of the next videos, they're
also called called arguments. Now after we do this here's the point when in most
programming languages you'd open your curly braces and start to code, but instead
with Python where we don't use those curly braces we just put a colon here. Now
this is going to be smart,
I mean Python's smart because it's going to automatically determine when this
function is complete based on indentation, so based on that you can
probably assume how we're supposed to use this, but we can't enter you'll
notice that it automatically indents four spaces in. This is because anything
four spaces in from the left hand side as of right now is going to be the top
level of this function and then you can further intense stuff, and we're gonna
explore that as needed as we go on. So this is going to be just a very basic
function that prints something out, so let's go ahead and print out this is my
function, and then we come down here, and we removed the indentation telling the
function that we're done. So now anything we write here is not considered
inside that function. Now we could, we could additionally
put more in this function simply by doing that and writing more indented to
the fourth space in, and now also with Python this isn't something that I've
mentioned yet i don't think but you don't need to end statements with a
semicolon liking in a lot of other programming languages, so just keep that
in mind. Now we can drop down out of that, out of that function and now we can call
that function. So to call a function you're just gonna type the name of the
function, and then any parameters that you're going to pass in which would go
in these brackets. In this case we're not going to put anything in these brackets
because we're not passing in arguments, we're going to be over that in the next video.
Now actually you can see PEP 8: no newline at end of file which means we
need a new line at the end file. You'll see that the issue has been resolved. So
PyCharm will kind of let you know if you're ignoring or just not using any
pep guidelines in your code. So let's go ahead and save this and let's go
ahead and run, and as you'll see down here in the console it first printed out
this is my function, and then it printed out a second string. So if we look up
here at the code again we're going to go over the flow of this one more time.
We're defining a function, we're function my function so that we can call
it by that name, and we're putting brackets here which will contain
arguments at some point, you don't have to, If your function doesn't take
arguments, it doesn't need outside information, just don't put anything in
there, and then a colon which starts the indented block of code on the next lines
which is indented four spaces. Now, I mentioned further indentation like in a
function like eight spaces, 16 spaces,
that's for each block of code, and that's not really something I can explain
further, you
guys are going to see it as we go on. So anyway this is the function
because they're both invented to four lines of Python knows that's inside the
function, and then down here outside the function we're calling the function
making it printed out. So, in the next video we're going to talk about
arguments and how to use those inside your functions.