Python functions,
these are really important to know.
So most programming languages have this thing called a function,
and we've actually used these a few times, such as 'print',
we've used 'type', we've used 'input',
we've used 'enumerate'.
These are all functions, but these ones all come built-in
with Python.
We can also create our own functions and name them anything we want.
Now, a function is really just a piece of code that takes
a variable and, I don't know, performs some sort of action,
does some sort of logic, and spits out an answer.
So if there was a Python function that was called 'add_two_numbers',
and you put 1 and 45 in there, you would expect
this to then return the number 46.
Well, we're going to learn how to basically create this because
this is not a Python built-in function, but we can create
something like this, and we can make them super complex,
or we can keep them super simple.
But first things first, let's go ahead and take a look at
some of these syntax.
Whenever we create a new function, it always starts with
the three letters 'd-e-f', 'def'.
So we're going to define a new function, and we can call it
'func_name', and we can have parameters in here.
So just as an example, let's keep this simple, and let's not
have any parameters in here.
So we have a function here called 'func_name', and it's supposed to
do something, so 'print("Hello")'.
And now to execute this 'func_name()', we just type 'func_name()',
[no audio]
and it prints out "Hello".
And that's literally all there is to a function, and
we can make this more complex.
We can add more logic in here.
There are some extra caveats like scoping, but we'll talk
about that down the road.
But essentially you just created a function very similar to 'print'.
Now we can create another function, and let's actually create that
'add_two_numbers' function,
and this thing can have a parameter.
So let's say 'num1' is going to be the first parameter,
it's going to be the only parameter right now, and it's going to
'print(answr)', whatever
that's going to be, the 'answer'.
And I'm going to create a variable in here called 'answer',
and that's going to be 'num1 +',
and then just a number
I'm going to tell it to add, so 3.14.
And then we can say 'add_two_numbers',
and then we can put a number in here.
Now this is called an argument or a parameter.
And all we're doing is saying, "Hey, in this particular function,
let's throw in some extra data", and then we can use this
exact variable name inside of this function.
So let's go ahead, and just say 80.
Now this is going to run 'add_two_numbers' together.
This is effectively the number 80, and the 'answer' is going
to be 80 + 3.14,
and it's going to print that 'answer' out to us.
So should this work, this will say 83.14, just like that.
And that is effectively an argument or a parameter.
Now, if we wanted to, we could add two parameters in here.
And I'm going to keep this as the first example, and use a different
cell. 'num1',
and let's go ahead and add a second parameter, 'num2'.
Let's simply say the 'answer = num1 + num2',
and this is actually going to add two numbers together,
two custom numbers.
[no audio]
So 'add_two_numbers'. What numbers do we want to add? Say, 42 and 24
Sure enough, we got 66.
It added our numbers together.
This is great and all, but there's a problem.
We actually can't get 'answer' outside of the function anymore.
So if I type in 'answer', simply does not exist.
This is a scoping problem.
We'll talk about scoping in a future lesson, but essentially
we created a variable here that we don't have access to.
And let's say we wanted to store this number 66 or 83.14
in a new variable, and work with that down the road,
how do we store this in a variable?
Well, up until now, we would simply put it in a variable.
We would say the 'total = add_two_numbers',
and I'll just move that up.
'add_two_numbers', and let's add 12 and 34.
Okay. That printed out the total of 46, but the total value
of this particular variable is not 46.
It's not what you expect.
It's actually nothing right now.
So let's do 'type(total)', and we're going to see this is a
'NoneType' as expected.
So something to keep in mind with a function is,
a function always comes with this idea of returning an 'answer'.
So you give it some data to work with, and it returns an
answer to you, much like a calculator.
So you punch in 90 divided by 3, and at the end it just says 30.
Now, we do that with this keyword 'return', and then we could
return anything we want in here, "anything we want in here".
So let's take an example of this 'return' keyword.
Let's see how this works.
So let's create a new function in here, and let's call it
'get_name', and it's not going to take any parameters,
and all it's going to do is 'return "Kalob"'.
Now, if I do 'get_name()' with the parentheses because it's a
function, okay, it returned 'Kalob' as a string.
So that's good.
But I don't have a variable to work with here.
So if I wanted to store this in a variable, I could say 'name
= get_name',
and then we could 'print(name)'.
We could 'type(name)', which is going to be a string, because
that's what we told it to return
here. Quotations means it's a string.
And now we're able to store function logic in a variable
using the 'return' keyword.
Now, let's go ahead and create a custom greeting function.
So let's do 'def greeting()', and it's going to take a 'name',
and this is simply going to return an f-string.
"Welcome to Python for Everybody",
and then the ''.
[no audio]
And if we run 'greeting' with a 'name' in here,
'Henry'.
Okay, cool. But that's still not in any sort of variable.
So we could now assign this to a variable saying, I don't
know, the 'sentence' or the welcome sentence is going to be
'greeting('henry')', and let's make that capital 'H', because it's a name,
and then we can use this later.
So 'sentence'.
And why is this important?
Because now we can do things like 'sentence.',
we can 'count' the number of times there's an "o" in here.
There's five "o" in the sentence.
We can do things like 'sentence.upper'.
And so effectively we've performed some sort of logic in
this function called 'greeting()'.
And then with the 'return' keyword, we were able to store that
in a variable, and now we can do anything else with that variable.
Now, whenever you see this 'return' keyword, it is a lot like
the 'break' keyword in a loop,
any code below it is not going to execute, and it's going to
immediately exit the function, just like 'break' immediately
exit a for loop or a while loop, really, any sort of loop.
So just as an example in here, let's do 'print("WILL THIS RUN?")'.
Because we've noticed that the 'print' statement up here, they
were they, were working perfectly fine.
As soon as we called 'add_two_numbers', it would
print the 'answer' just like this.
So if we run 'greeting()' here, this should say "Welcome to Python
for Everybody", whatever the 'name' is.
So, "Henry". And ,will this run?
We don't know.
So let's go ahead and run this, and run that, and it does not
run. And that's because effectively, Python says there's
a function in here, and it hits a keyword called 'return', and
it essentially thinks that it simply does not exist.
Python does not care that there's anything below this 'return' keyword.
[no audio]
Now, the reason we want to use functions is because
if we have any sort of logic that we're planning over and over
again, and we wanted to be able to change over time,
and change throughout our entire application, we can change
it in just one spot.
So let's create a function in here called 'can_drink',
and it's going to take an 'age',
and we're simply going to say 'if age >
18: return True
else return False.
Now, where I'm from, if you're 18 or older, you can legally drink alcohol,
and this works for lots of places across the globe, but not every place.
So we could say, 'can_kalob_drink = can_drink()',
and then I'll put my age of 30 in here, and 'can_kalob_drink'
is going to be True.
30 is in fact, greater than 18.
So really all this did was say, "Put 30 in here.
Put 30 in here.
'if 30 > 18'
Yes, of course it is.
So 'return True'.
If this was False, if this was a lower number, this would
not be executed and it would run the 'else' statement, and then
'return False' would be the answer.
So we can actually take an example of this, and let's say,
15, and I can no longer drink.
Now, the reason you might want to do something like this
is because in your application you might want to make it
available to lots of people across the world.
But in the United States, you have to be 21 or older to drink,
and actually this should be '>='.
But in the United States, you need to be 21 or older to drink.
So what happens if we constantly wrote in our application,
'if age >=18', do a thing, and then
somewhere down the line, let's say 300 lines of code later,
we have it again, 'if age >= 18',
do some more stuff.
[no audio]
We could also do somewhere down the line again, maybe even
in a different file, 'if age < 18', "#cannot drink". Right.
So we have all these different use cases where we are using
the same logic over and over and over again.
What we can do instead is put this into a function.
And so we can say 'if can_drink()', and then the user's 'age' is,
I don't know, 30.
And we could do it again here.
So I'm just going to copy this.
[no audio]
And because this one is less than, we can say, 'if not', because
that's going to 'return False', cannot drink.
So now we're actually cleaning up our logic because down
the road you can simply change this to 21,
and now it affects all three of these areas.
You don't have to change
[no audio]
18 to 21, to 21, to 21, you don't have to do that.
You can just change it one spot.
Now that's the power behind a function.
We can even get more clever with this and assign it to a
variable and only ever reference that variable instead of
referencing the function over and over and over again.
But that's a little bit boring.
So let's talk about more parameters, more arguments.
Let's look at a 'default' value.
So 'def greeting()',
and let's say we are asking for a 'name'. 'print("Hello,"', and
then the 'name'. We can run 'greeting('Kalob')',
and it's going to say, "Hello, Kalob".
But what if we didn't actually pass in any argument?
We get an error "greeting() missing 1 required positional argument: 'name'".
So it's saying that this 'name' variable is actually missing.
This parameter, this argument is missing.
What we can do instead, is give it a 'default'.
So the default is going to be "Zephyr" in this case.
So if there is no 'name', it's going to default to "Zephyr".
So let's run this cell, and run that cell, and it says, "Hello
Zephyr", but if I specifically overwrite that by giving it
a specific value in here, I can overwrite the default value.
So 'name' is always going to be "Zephyr" by default unless you tell
it otherwise.
In which case this says, "Hello Henry", because we have overwritten it.
So why or how is this even useful?
Let's go ahead and create a new function in here, and it's
going to be called 'create_person',
and what this is going to do is return a dictionary of data
for us. So we could say it needs a 'name', needs an 'age',
[no audio]
The 'role' might be a 'Student'.
Maybe this is for a learning platform,
and so every 'role' by default is going to be a 'Student'.
And let's also pass in the required number of books that
every student needs to have.
And let's say the required number of books, 'required_books'
is going to be 5 by default.
What we can do here now, is we can create a 'person =
a dictionary, and we can say the 'name' is going to be whatever
this 'name' value is;
the 'age' is going to be whatever that 'age' value is from that 'age' variable;
the 'role' is going to be whatever 'role' is,
and by default, that's always going to be 'Student',
so that's always going to be filled out; and 'required_books'
is going to be whatever 'required_books' is.
Now, these keys in here just happen to be the same name,
they don't have to be the same name.
So we could say 'person_name' just as an example,
and 'total_books = required_books', something like
that. And then we could return this entire dictionary.
[no audio]
So now I can create a 'person' called 'kalob'. 'create_person()'.
My 'name' is going to be 'Kalob',
my 'age' is going to be 30,
my 'role' is going to be, I'm going to overwrite this, 'Teacher',
but I'm going to keep 'required_books = 5'.
So 'total_books' is going to say 5.
And when I print out 'kalob', you can see 'person_name',
that's the key here, matches 'Kalob',
that's here; the 'age' is 30;
the 'role' is 'Teacher'.
We actually overwrote the default value for 'role'.
And fun fact, we use a little named argument here.
So we made sure that it was specifically overwriting 'role'.
And the 'total_books' is 5, which we didn't specify at all.
The 'required_books' was not a parameter that we gave it, but
it still used 5 as a default.
Now how is this useful?
Well, we can now say 'nathan = create_person(
"Nathan", 29,
his 'role' is a 'Student', and the 'required_books' he needs
is actually only 2, so we can overwrite that.
And let's take a look at what 'nathan' has. First 'name' is "Nathan";
'age' is 29; 'role' is a 'Student',
so we actually didn't need that at all;
and the 'required_books' is 2.
So let's rerun this.
And it didn't change.
We got rid of 'role = 'Student', and it still just
became the default.
So a function essentially allows us to write all of our logic
in here just once, and then down the road, we can call it
with one line of code, and it will always create and always
work with these five lines of code here, or six lines of code
rather. Now, what I would like you to do, is create
your own function.
Go ahead and create a function that asks for user's age, cast
that value as an integer, and then make the function say,
"You are X years old".
So as a quick little recap, you're going to want to create
a function with 'def',
then the function name, whatever that 'func_name' is going to
be, it needs to take an 'age'.
So 'x' could be the 'age',
or you could just call it 'age',
and then you need to 'return' or 'print' a string that says, "You
are", and then whatever that '' is, "years old".
Last but not least, what I would like you to do is create
this function and then put it in a '.py' file, in a '.py'
file, and then from your command line I want you to run
'python yourfile.py', whatever your file is going to be called,
and you should end up seeing your sentence in here.
Don't forget, you need, you're going to need to call your
function, so just because you have assigned it does not mean
that it's actually going to run.
You're going to need to do something like this.
[no audio]
So go ahead and give that a shot.