11. How to create custom functions in python
Oct 8, 2023
Python functions are super helpful so we don't have to let the same line of code over and over. Instead, we can write the code block once and call it over and over. Functions are flexible and they can accept different numbers of parameters and even varying numbers and types of parameters or parameters with key values or default values. This video gives an example of how we can use these to convert Fahrenheit to Celsius and a custom one using a Lambda function which is essentially a one-line version of a function.
In this video, we're going to talk about functions in Python.
Functions are super cool.
They're these little mini blocks of code that you can call on,
and it will run that code wherever
you're at in your program.
This is great.
It means you only have to define something once,
and then you don't have to bother about constantly
redefining it.
You just call it each time.
We use it by using the def keyword.
That's how we define our function.
We send it parameters, and then the function
can, if we wanted to, return a value, or even more
than one value, which is so handy.
So let's do a quick example of this.
Let's say that I'm going to be doing a code project where
a bunch of temperatures are going to come in,
and they're probably going to be in Fahrenheit.
And so I need to convert those to Celsius
because we're not barbarians.
So that process is a little bit tricky each time.
It would be cumbersome to write out the formula.
What if we just wrote a function to do it
every single time for us?
So we're going to define a function.
We'll call this define, so D-E-F,
and then we should name this something.
So let's just call this F2C.
We're going from Fahrenheit to Celsius.
This is bad, by the way.
You should do descriptive titles, not
nonsense ones like this, but we're going to do it.
And what it wants is a temperature.
That's the variable that we're going to be sending it,
so we need to put that inside of here.
So now we come down here, and we're
going to do the math with it.
So our new temp, the new temperature,
is going to be equal to the temperature minus 32,
because that's how you convert from Fahrenheit to Celsius.
You take that number, you subtract 32 from it,
and then you multiply by 5 9ths.
And then, before we're done, since we actually
want to get that number back, the new number in Celsius,
we need to hit the keyword return,
and then type new temperature.
That's what's going to get sent back to us.
So that's it.
That's all there is to it.
Now let's try and use this.
So let's define something.
Let's say T1 is our first temperature.
Let's say that it's 32 degrees Fahrenheit.
So when we convert this, if we've done it correctly,
that should come out to 0 degrees Celsius, freezing.
So let's go ahead and do this.
We're going to say, we'll say this temp in Celsius
is our new variable.
This is going to be equal to the function f to c of T1,
where we're sending it.
T1 is our variable.
Now it doesn't matter that T1 is not the same name as temp
up here.
It doesn't matter.
In fact, variables inside of your function
are what we call local variables.
They don't exist outside of your function.
These ones out here are our global variables.
They're the ones that show up in this window over here
when we run the program.
In fact, if you want something to be a global variable over
here inside your function, you have
to declare it with the keyword global.
So we're not going to bother that.
So let's go ahead and run this.
So hit F5.
And sure enough, here's our first variable.
It was T1.
It was equal to 32 degrees.
Then we solved for Tc by running it through our function
by sending 32 up to here.
And then it came out as 0.
Let's double check it and make sure
that somewhere around room temperature, like 75 degrees,
comes out.
I think room temperature Celsius is around 22 or so degrees.
Let's run it.
Yeah, 23.8.
So this looks like it's working fine.
OK, what else can we say about functions?
This is it in its most sort of simple form.
But it can do quite a bit of other things.
So for example, one thing you need
to be aware of with functions is that you
need to send it the exact number of parameters
that it's expecting.
So let's say we've got another temperature over here
called T2.
And this one's 50 degrees Fahrenheit.
Well, if I try and send it T1 and T2, so if we try it,
we're going to see an error.
Yeah, it says it takes one positional argument,
but two were given.
So it didn't like that.
So you need to make sure that you send it the right number.
Now, if you don't know how many parameters that you're
going to be sending it, you don't quite know ahead of time,
you can modify this.
You can put a star in front of the parameter in the function
when you define it.
And that will turn these parameters into a tuple.
You still need to make sure that you call on just one
of them in the tuple.
For example, if we do it up here,
we're going to put a star in front of temp.
Now, it's OK with receiving multiple values,
but we need to tell it which one we're going to do right here.
So let's say we wanted to do it on the one with index 0.
Now, no problem.
It's going to run it, and it worked perfectly.
And if we wanted to do it on the second one, the one at index 1,
no problem, we can do that.
And again, here's our new value.
It's down at 10 degrees.
So that's how you can send arbitrary number of parameters
in if you don't know how many there will be ahead of time.
But there's other things you can do.
When you're working with functions,
you can send key value syntax.
So for example, when we have temp here, we could do this.
We could say that temp 1, we're naming it a key and a value.
So that's temp 1, and this is temp 2.
So now we have not just variables,
we have keys and values being sent.
So you can do that.
You just need to tell it which one it's expecting.
So up here, we would need to say,
OK, you're going to be receiving temp 1 and temp 2.
And then we could say do the math on temp 1, for example.
We do that and run it.
Sure enough, it changes the value back to what it ought to be.
So you can use keywords here.
That way, if the order gets messed up, it's no problem.
We can change this order.
Right now, temp 1 and temp 2 are there.
We could switch this up here.
We could have temp 2 and temp 1.
And because we used keywords and values here, or keys and values,
there's no problem with it.
It still runs just the same.
So that's something you can be aware of.
In fact, it's even possible to send
an arbitrary number of keyword parameters
if you put two stars in front of it.
So let's do the same thing again.
Instead of having temp 1 and temp 2 in our function,
we're just going to put temp.
But we put two stars in front of it.
And that tells the function, OK, something
that's going to be coming in is going to be key and value.
So here's our keys and values.
And so down here, when we use it,
all we need to tell it is which one to look for.
So let's tell it to look for temp 2.
If we run that now, no problem.
It switched it down to 10 degrees, which it ought to.
This was 10 Celsius.
And this was 23 Celsius, I think it was.
What else can we say about these?
It's possible to give your function a default value just
in case nothing gets sent.
So let's try that.
Let's do the following.
Let's say that temperature is equal to, we'll say,
15 degrees.
And that way, if we send this with nothing,
if we remove all of those and we run it, no problem.
Oh, here, it's still looking for the key value terminology.
So we need to get rid of that.
So when we run it, no problem.
It converts it to the Celsius value.
Your function cannot be empty.
That will throw an error.
So if you think you're going to work on it later,
but you don't know what you're going to put in there yet,
make sure you use the pass keyword.
You could put a pass in here, and then you don't get an error.
And then there's this really cool thing possible
with functions.
You can do recursion.
Recursion is like inception, a dream
within a dream within a dream.
So when a function calls itself, that's
what we call a recursive function.
So these are cool, and we can allow these to do cool things.
This is sort of a ghetto way to do loops.
We'll learn about loops in our next video.
But heads up, it is possible for it
to just keep on calling itself, and then it never escapes.
So you want to avoid that.
So let's look at an example of a recursive loop.
OK, let's come down here.
We're going to make a new function.
We'll call it define our function try recursion.
This is going to take as an input a value.
Let's call it i.
So what we're going to do is we're first
going to ask if i is greater than 0, for example,
then we're going to do something.
And what we're going to do is let's say our result is
equal to i plus.
We're going to now do a recursive function
by doing try recursion.
And we're going to send it the value of i minus 1.
And then we can print result.
Now, that's all happening as long as i is greater than 0.
So as a number comes in, let's say it's like a number 5,
you can see that it will initially be bigger.
And so the result will be 5 plus the number
of when it runs through this again.
And then we're going to print that.
So the value of i is going to be getting smaller and smaller
each time.
So eventually, i will be less than 0.
So let's come over here and tell it
what to do in that scenario.
So in that scenario, we're going to use else.
I have to make sure that my tab notation is correct.
We want it to happen at the same level as this.
In the scenario where it's less than 0,
that's when we're going to return result, something
that will leave the loop.
First, actually, let's say result is equal to 0.
Then we're going to return our result.
And we're going to leave.
So let's try this out and see how it worked.
So let's try sending something to this.
So we're going to do try recursion.
And we'll try sending it a value of 5.
Let's see what happens.
We're going to run this.
Ah, I think right here, this needs to be indented to there.
OK, now let's try that again.
OK, so sure enough, here's what it did.
You can see each time when it ran through,
it printed the result until we reached the point where
it was less than 1.
And so it escaped.
So these can get a little bit tricky.
There's really better tools.
For loops are typically better.
But you can have recursive functions
which call on themselves.
All right, the last thing we're going to talk about
are lambda functions.
Lambda functions are cool.
The word lambda, it's a key word in Python
that just means heads up, here is coming an anonymous
function.
We use these to make little one-line functions.
Rather than defining the function up top,
and then in your code, calling that function
where it's to go back and use it,
it's just all in one little line, a little function.
So for example, let's say that we wanted to do this.
We wanted to sort the list of words
by a number of unique letters in those words.
So we've got a list of words, right?
And we want to sort them by the number of unique letters.
This would be the syntax for that.
Well, let's actually just type this out.
So we're going to make a list.
List of words is equal to member,
because we're using a list, I need the square brackets.
So we'll do cat, puppy.
We'll do bat, monster, OK?
So now we can go ahead and use a lambda function.
So we'll call this our sorted list, right?
Is now equal to, we're going to use the sort function, where
sort has a key, right?
And there's some pre-built in keys,
but we can write our own function here.
So we're going to do lambda, OK?
So now it knows that it's a little mini function.
We're going to say x.
We're going to do the length of the set of the list of words,
like so.
Let's try that.
So now out comes this sorted list.
We needed to do our initial list of words,
because sort is a method.
So remember, a method, you type the variable in a period,
and then the method, so we lift that off.
So we need to do list, underscore, of, underscore,
words, dot, sort dot key.
Now let's try that again.
Ah, I see.
We needed to just make a list there.
OK, there we go.
Three and three, so these are equal.
So it makes sense that they're both,
one's not better than the other.
But this one has, what, six different ones,
and this has more.
So OK, so that's working.
So that's an example of a lambda function.
Again, it just called this little mini function
right here in line with the other operations.
So these are really useful, and you'll
see these used quite a bit, especially if you're
going to have lots of different functions.
You don't want to declare so many of them.
You can just do them right in line with your code.
So that is functions in Python.

