-: Hi guys, and welcome back.
In this video, we're going to learn about functions.
We've been using some functions
such as a print and line and sum,
but now we're going to be defining our own.
And that means that we can put some bits of code,
and give them a name, so that we can re-use
those bits of code whenever we want.
Let's learn more about them.
I'm going to create my first function,
and I'm going to call it hello.
So you start off by using the def keyword,
and then you type out the name
of the function you want to define.
Then you put a pair of brackets, and a colon,
and that begins the function body.
Here we're going to put print, hello.
And what happens now is that we have a function created.
When Python reaches a def keyword, it then says, okay,
we've got a hello variable that we are creating.
Only this variable is not a normal value variable
like a string or an integer, this is a callable variable.
That means that it's a variable whose value is a function.
And a function you can call or execute.
Calling, running, or executing a function are all synonyms,
so they mean the same thing.
So when you define a function,
you create a callable variable.
Now creating the function or defining the function
does not run the function body.
It just tells Python that a variable called hello exists.
So if you run this code, nothing actually happens.
As you can see down here, nothing is printed out,
because the function has been defined,
but the function has not been called.
So in order to call the function,
you have to type out the variable name, which is hello,
and then the brackets.
And that, these two brackets after a variable name,
is what actually calls the function and executes it.
The order of running these lines in Python is as follows.
First line one runs.
And it tells Python to create this hello variable
that is a callable.
Then, line two does not run.
Line three runs, but it's empty.
And then line four runs,
which tells Python to execute this function.
So Python jumps back up, and then runs line number two.
Notice that Python knows line number two
is part of the function body,
because of course it is indented,
so it's got these four spaces in front.
You can put more lines in a function,
as long as they are also indented, if you want.
Let's define another function.
Here I've created a callable variable, user age in seconds.
And it is a function.
And now, we can put some code inside of it.
So we're going to implement the first app
that we coded earlier on.
So user age is going be int of input, enter your age.
Here we ask the user for their age,
and then we're going to turn it into seconds,
so that we can print it out.
So age in seconds is going to be user age,
times 365, times 24, times 60 times 60.
That is years, multiplied by days,
hours, minutes, and seconds.
And then we're going to print it out.
Just like that.
Of course, just to reiterate,
if we run this, nothing happens.
You do have to call the function, user age in seconds.
And if you do that, you can actually type down here.
Let me repeat at this point that
sometimes I'm pressing the play button,
and other times I'm running the code in a terminal,
because pressing the play button does not allow you to type.
So you have to run this code in a terminal
if you want to type.
Let's enter the age, such as 30,
and then we get your age in seconds is that.
Now one of the benefits of doing this
is that what you have now are two
reasonably separate bits of code.
One, which is what the code does, and the other one,
which is the code that we'll actually run.
Now this is very much a conceptual separation.
So the code is really just a single file,
which runs line by line and so on.
But conceptually, as a person,
you can think of it as running the code,
and defining the code.
So it can help you understand and read through your code
a bit better if you make that conceptual separation.
For example here we're running the code,
so we could actually add some more things.
Such as that.
Now if we run this again, you get a nice message.
And then you get your goodbye there.
But as I said earlier,
these lines here are just running from top to bottom.
So you can move that line to the top if you want.
And you can put this line anywhere you like.
And Python will still print out the exact same stuff.
Welcome to the age in seconds programme
will be printed out first.
Then the user age in seconds will be printed out next,
which runs through this function,
and finally goodbye will be printed out after.
However, by treating these two as slightly separated
parts of code, it can actually be much easier
to reason about, much easier to read,
and much easier to think about.
So that is just a tip there from me to you.
A couple of things that you should not do
when you're working with functions is re-use names.
So if you define a function called print,
and then you try to print hello world,
you're going to get a problem.
Because when you define the print function,
that creates this print variable that you can use,
and then you're actually using it, here.
This is no longer the Python built in print function
that we're calling, this is calling your own function.
And this is almost never what you want,
although it is valid Python code,
it's almost never what you want.
And another reason why you get an error by doing this,
is because you're putting in a string inside the brackets,
but this function cannot take in a string.
We're going to learn how to do that in a future video.
So if you run that, you're going to get an error.
It says, print takes zero positional arguments,
but one was given.
That means that we've put a string in there,
but our function does not accept a string.
Another thing that you may want to avoid,
is imagine you've got a list of your friend names,
and then you have a function that adds a friend.
Then you take in the friend name from the user's input.
And then here's something
that you almost definitely want to avoid,
and that is to say friends equal friends plus friend name.
What's happening here
is you're re-using the friends variable
in two different places.
And because you are inside a function,
Python actually creates a new space for variables
that is separate from the global space,
so functions have their own little space for your variables.
And when you do friends equal,
Python is creating a new variable called friends
that only exists inside the function.
The same thing with friend name.
So, you now have two variables.
Friends, in the global scope of the file,
and friends in the function scope
of this add friend function.
This is almost never something you want to do.
Because even though you are using a variable called friends,
you are also defining a new variable.
So let me run this here.
So let me run that, and you can see that if I enter Rolf,
you get your error here.
And what's happening is that this variable exists.
And if you call this something like F,
then this code is fine.
Because Python will see the friends variable.
It will check whether it exists inside the function,
but it doesn't, so it uses the one outside the function.
So that's fine.
But as soon as you do
friends equal friends plus friend name,
Python is going to look at this friends variable,
and notice that it's being defined inside the function.
And you can't do that, because you can't use a variable
in the same line where you're defining it,
it doesn't makes sense.
So it will give you this error.
It's akin to doing X equal X plus five,
but X doesn't exist.
So Python will also be very confused if you do that.
Alright, so almost never will you want
to shadow a variable name from an outer scope.
That is a technical definition for what we're doing here.
We are shadowing the friends variable
by redefining it inside a function
where it already exists on the outside.
Again this is valid Python code.
Of course this is not,
because you're using the variable
in the same line you're defining it.
But generally, shadowing a variable is valid Python code.
It's just something that you usually don't want to do.
Something else you can't do with functions
is use them before they get defined.
Maybe this is obvious, but here we're calling the function,
say hello, and then we're defining it
down here in line four.
Python won't let you do this, if you try to do that,
you get an error saying
that the say hello name is not defined.
Here we've got a final example
where we have an add friend function
that takes in a global variable, friends,
and appends Rolf to it.
Then we define the friends variable in line five
after the function's been created,
then we call the function add friend,
and finally we print friends.
So this may look like an error, because here in line two,
the friends variable has not yet been defined.
But actually remember that this line of code only runs
when line six runs.
So this is fine.
Although a bit confusing.
The friends variable that this uses
will be defined by the time this line is ran.
So that's why this will work.
Although it will be quite confusing as I said earlier.
Usually you'll want to put your global variables at the top,
so that it's a bit clearer what's going on.
Also this line here is modifying the friends list.
So if you call this function multiple times,
it's going to append Rolf to this list multiple times.
So that's something interesting about this function here,
that you are appending to this list,
so you'll end up with multiple elements,
if you run the function multiple times.
Alright that's everything for this video,
I just wanted to show you
what functions look like in Python, how you define them,
how you use them, and some things that you don't want to do
when you're working with functions.
Thanks for joining me in this one,
and I'll see you in the next one.