Hey, in this video, you will learn one of the most important elements of Python, and that is custom
functions.
So let's look at our code and see how custom functions.
Will improve our program because currently we have a problem with the program and the problem is redundancy.
So we have repetitive codes.
In our code base.
These two lines here are repeated in here.
And here as well.
And here too.
So four times we have the same code.
There certainly should be a better way not to have to write repetitive code.
And the answer is custom.
Some functions.
So now I'll go ahead and create a custom function and you'll see how a custom function becomes beneficial
in avoiding redundant code.
Custom functions are usually created on top of your code, so outside of the loop.
So it costs and function is like a slot where you put some codes and then you just call that custom
function.
So the slot must have a name.
So let's call this get to dos.
And that's the syntax.
That's how you start.
So you start with the def keyword and then you specify a name, whatever you like.
The rules of naming it are the same as naming variables, so you shouldn't start with a number.
You shouldn't have spaces in the name.
You can have underscores.
That's fine.
So that's the first line.
In the next line, then you put the code.
So in our case, it would be these two lines.
So I'm going to copy them and paste them in here.
But we need indentation.
So here you want to paste.
That's code.
So make sure it's for spaces.
For spaces is the recommendation.
So I have four spaces here.
One, two, three, four and eight here.
One, two, three, four.
So this indentation here is because we have this width context manager and this indentation here is
part of the function.
Make some space.
It is recommended to have two spaces between a function and other parts of the code.
So two break lines.
So that's the code that is repetitive.
We put it in a function and then we want to add another line at the end which starts with return.
So return is a statement just like we have other keywords.
So this is also a keywords.
So a return to dos.
So that there is the function definition.
Once you have defined the function, then you can remove these lines and replace them with the function
call.
So that's like the other functions we have been working on.
Like you see open input print.
So this also has the same syntax.
Now it doesn't get any argument.
You'll see how to create functions, which also get arguments.
For this lecture, let's focus on functions which don't get any arguments.
So this gets to do.
Function will return something.
Right.
That is the list.
Now we need to store that into a variable.
The variable is normally this one to dos.
So we store that list in that variable there and then we append to dos to that variable.
And we can do the same in the other parts of the code.
So to do is equal to get to deuce.
So whatever you have this function in reads mode, write, don't confuse it with write mode.
We will work on that later.
That's not the same code, correct?
We should be clear about that.
So here is another open function with the read modes to do is equal to get to dos with parentheses.
Don't forget the parentheses because that is a function called.
And the last one to do so is equal to get to deuce.
And that's it.
So let's run the codes to make sure it's working.
Let's add say to do
make soup
make pizza, enter show and everything seems to be working fine.
So we tried the show condition where we have this function and everything is working fine.
So.
You know, that we had these built in functions in Python, such as open and input.
So I've been referring to these as functions, but these are actually function calls.
So here we call the function.
The function itself is written somewhere else.
So just like here, this is not the function.
This is the function call.
The function itself is written in here.
So that's the instructions on what this function does.
Now, where are these functions written?
Well, if you are on patch or you can right click here and you can go to go to and go to implementations.
If you are in another ID, then just watch the video.
You don't have to perform the same.
So just see what I'll explain here.
So by going there, this will take you to the function definition for the input function.
So again, we can try another function such as open right click go to.
Implementations.
It takes you again to the definition of the open function.
If I press this asterisk, it will expand the definition.
So this is just comments.
This is not code.
It's the description of the function, which is actually what you get when you do help open in the command
line.
So if you do help open, you actually see the same description
and that is referred to as a docstring.
So it's the documentation of the function.
And then down here you have paths so you don't actually see the code here, you only see this docstring
and.
Pass.
And that is because all these functions so open and input, these were written in C language.
So C is the language.
Used to ride the interpreter of Python.
Remember when we installed the interpreter from Python dot org?
That is a program which interprets Python language and that program is written in C language.
So all these functions are actually written in C language and we cannot see the code.
You'd ask, Well, what is this then?
Well, this is just a fake representation of the function and this is done by Python, so other IDs
may not offer this feature.
So this is not so much useful.
Just wanted to show you that these are actually written somewhere.
In this case, it's in C language.
You'll see that other functions which will use later in the course are written in Python actually,
but not these main functions.
So let's close the console.
And next, I want to talk about variable scope, to be exact.
So if I was to open a new python file here.
Just give it a name, whatever you like, and create a new function.
Such as?
Greet.
This function has a variable which has a value.
Let's say hello.
And then maybe another variable you message equal to message thought.
Capitalize and return new message.
Meanwhile, I would like to right click here and keep our program open.
Here on the right.
So maybe you want to see that, too.
To compare the functions.
So both functions have a def keyword and return statement as well.
The syntax is the same.
They both have a variable declaration.
In the body.
This one has two variables here, here and here.
So now I can call this function.
Let's say greeting is equal to greet and then print out greeting.
And we need to make one more break line there for being consistent with recommendations of style and
run the code.
Yeah, run test.
And that's the output.
Hello with Capital H.
So what's happened is that when the function is called, then this is returned.
And what is the value of that?
Well.
So here message gets this halo string and then the value of new message will be Halo with H capitalized.
And so the function returns Halo capitalized.
Therefore the value of this is going to be Halo.
And that value is stored in the variable greeting here.
And then we print out that value.
So we get.
Hello.
So far, so good.
What happened to though, if we print new message and this is where we talk about variable scope.
So if we run this program, we're going to get an error.
It says Name, error, name.
New message is not defined.
How comes since we define it here?
Well, that's because this variable.
Is only valid inside the grid function, so its scope is local.
That's why you cannot access that variable from here.
You'd ask, Why is that?
Well, let's take an example.
Let's say you go to a cafe and you go to the bartender and you order a coffee.
You just say, I want a coffee.
So these words are the function call.
You just call the function.
The function itself is the process of making the coffee.
So the codes of the function, the elements of the function definition are maybe coffee beans.
It's the machine, the water, the interactions between all these.
And it's not a good idea for you as a client to have access to these elements which make up the function,
the coffee process.
So the function is the coffee making process.
So all you get as a client is the coffee.
That's what we get also from this return.
Statement.
So the written statement makes available only one variable, the output variable that you want the function
to produce in our coffee example, that would be the actual coffee cup with the coffee inside.
So that is the output.
Likewise, you cannot access the message variable.
So every variable in here is.
Not accessible.
So you can only access what is returned and you can access it by calling the function, not by referring
to the variable name itself.
That's an internal name inside the process.
Now back to our app.
We see I have this line here and this is saying that this name shadows to dos from alter scope.
Alter scope is.
The variables which are declared outside functions.
So in this program here, that would be, for example, greeting.
This is a variable in the outer scope of the program.
So the problem here in our app, which is not a problem, actually it's a recommendation, but we can
fix it.
The thing is that you we have this variable with this name here, and it's not a good idea to have this
variable with the same name.
So what we can do is we can change the name to this variable, let's say to this local.
And then of course.
Yea, be careful.
You have to return that same variable.
Right?
And so now what we have is we create the local variable in here, which will hold this list from to
dos.
And then we return the value of that.
Therefore, now this other variable, which is.
Not a local variable.
It's a global variable.
So the opposite of local is global.
So this variable now will be equal to the value that this variable produces in the function here.
We have the same problem with file.
So this is also complaining that we have another variable, a global variable with name file, so we
can convert this to local as well and also reflect that here.
So be careful, file locally or file local there.
So that's the final code.
Let's fix this complaint as well.
I can reformat the file and that will add a new break line.
So as I said, it's good to have two break lines between functions and other parts of the code.
So thanks a lot for following this and I hope it makes sense.
We will be using functions over and over again throughout the course, so let's keep going.