To begin
the first thing I want to talk about is more on functions.
So we've already seen a couple of things that we can do with functions.
The first thing I showed you was how to create a very simple bog-standard
function using the def, the name, the empty parentheses,
and then the colon. And then within the body of the function,
you can do a bunch of things which will be carried out every single time you
trigger or call the function like this.
And the first type of function basically just helps you reduce the amount of
code you have to write
when you have instructions that you want to execute repeatedly. Now,
the next type of function we saw actually had something inside the parentheses
and this something is an input which can be passed over when we call the
function. So in this case, 123
is the argument which gets passed into this parameter called something
and then it gets used within the body of the function.
The second type of function, the functions
which allow for inputs, gives us the ability to modify the code in the function
and to get it to do something different each time
depending on what input was passed in.
We're now onto the final sort of flavor functions
and these are functions
which allow you to have an output
once the function is completed.
We start off with a simple function, and in this case
you'll notice that even though I could, I'm actually not giving it any inputs.
The inputs and the outputs are completely separate from each other.
So this is a normal function that you've seen many times now. And within the
body of the function, I'm going to do some piece of calculation. Let's say
I'm going to calculate what is three times two,
and then save it to a variable called result. Now, once that's done,
I can use the output keyword, which is return.
And then after that keyword, I can put whatever it is
I want to be the output of this function. So in this case,
the output is the result.
And what that means is when I call this function later on and it runs,
then it will go ahead and output this result
and it replaces this line of code where this function was called.
Now that result is held in the code where the function was called and I could
save it to another variable. Result, of course, equals
3 * 2 and that means this output now equal 6.
So this is what the code might look like. Somewhere I would define my function
which has an output, which you can tell by the return keyword. Now,
at some other point in my code, I decide to call that function
and once that function executes, that return or whatever is outputted replaces
the function call. And at the very end,
it means this variable output will store whatever the output is.
So in this case, output would be equal to 6.
The way that I like to think about functions with outputs is almost kind of like
a machine, right?
If you had a machine where in goes some empty bottles and after some processing
or some inputs, outcomes a bottle filled with milk.
So in this case, this function has an input, the empty glass bottle.
It has an output, the glass bottle filled with milk and in the middle,
there's some processes or some code that's being executed to create this change.
This ties together all the things that we've learned about functions.
So now head over to the Day 10 starting replit,
and go ahead and fork your own copy of it. Now,
once you have your own copy of it,
we're going to create some functions with outputs of our own.
Let's start off by creating a new function
which I'm going to call format_name
and the reason is because this function is going to take a first name and a last
name, and it's going to change those inputs,
even if it was lowercase or if it was all caps,
it's going to change it into title case, something like this
where the first letter is capitalized in each word. To begin,
I want you to create this function and then go ahead and add two inputs to it.
One could f_name, so first name and another code l_name,
which is the last name. Pause the video and give that a go.
All right.
So all we have to do to add inputs to our function is just to put the names of
these parameters inside the parentheses.
So now our function is able to take these parameters as inputs
whenever this function gets called.
The next step is we're going to convert these strings that gets passed in,
so it could be something like this, all lower case.
It could be all capitalized, or it could be kind of random, right?
Like some are capitalized, some are not.
But we're going to make it all uniform,
so all of these are going to end up like this. And to do this,
we can search around in Google and you might come across a similar question in
Stack Overflow: How do you convert a string to title case in Python?
They have an example and it's trying to do exactly what we're trying to do.
If we take a look at the answers,
you can see that somebody is helpfully pointing to this title function.
So you take a string and then you write .title and then afterwards,
the string gets transformed and every word now starts off with a capital letter.
And if you want to know more about how this title case works,
then you can click on that link in title and then have a read through the Python
documentation. Once you've read through that,
go ahead and modify this Fname and Lname so that they end up being both in
title case. Pause the video and give that a go.
Right?
All right.
So we want to go ahead and take this Fname and then write a dot and then write
title to convert it to title case.
Once we've done that, then we're going to do the same with the l_name.
And if we go ahead and actually print these out,
and of course, in order for the function to actually execute,
we'll have to call the function. So I'm going to put two inputs in here,
one which is going to be, um, just angela, all in lowercase
and then the second one is going to be ANGELA all in up a case.
And now when I run this code,
you'll see that the result is they both get converted to title case.
In essence,
this part of the code becomes whatever the previous value of f_name is converted
to title case. You might have spotted already that this title function actually
has an output.
It returns a version of the string where each word is title cased.
Instead of simply just printing this,
we can also capture it inside a variable.
Let's create a new variable called formatted_f_name and also
formatted_l_name.
Now what's happening here is we are transforming this input f_name,
turning it into title case.
And then once this function executes,
it has an output and that output replaces this part of the code
and then it gets stored inside this variable.
Now we can go ahead and print the final version of our function
which is going to use an fstring to take the formatted Fname and then add a
space and then print out the formatted Lname, like this.
Now, if I start out with my first name
but all in strange sort of casing,
and then my last name in all caps,
let's go ahead and hit run and see what happens.
You can see that my first name gets converted to title the case,
my last name gets converted to title case
and then that result is stored in formatted Fname
and this result becomes stored in this Lname,
and then they get printed and formatted, like so. Now
instead of printing this result out,
we could also return it as well. In the same way that this title function
returns the output, replaces this part of the code,
we can also do the same with our format_name function.
So let's delete the print function and go ahead and use return
instead.
Now this formatted string becomes the output
and as we mentioned before,
what happens with functions with outputs is this output replaces the part of the
code where the function was called. Now,
if this is the string that we want to print,
then all we have to do is just to save the output
which replaces this part of the code inside a variable like this.
And then all we have to do is just go ahead and print that formatted string.
As you can see, when I run the code, the same thing happens. Now you can,
and you might've alternatively,
just taken this function call and put it straight inside the print statement.
That works exactly the same way.
And we can visualize this using Thonny.
If I go ahead and paste everything we have inside the editor and go ahead and
run debug,
then we can step into and see what are the steps that happen.
So the first thing
when this line 6 gets executed is it tries to call this function, format_name.
So it passes in these two inputs.
And we're now within the function where we have access to these two inputs,
Fname and Lname.
Now the formated_f_name becomes the title-cased
version of the first name and the same thing happens with the last name.
So we now have a formatted_f_name and a formatted_l_name
and that is what's going to be returned as the output.
Now, once that's completed, that output replaces
whatever was in between the print statement,
which is where we called this format name function
and this is what gets printed into the console. As I mentioned before,
we've actually already been using functions that we didn't create that have all
of these functionalities, right? For example,
the Len function takes an input and has an output.
The input is going to be a string,
my name again, seemed to be really obsessed with my own name.
So now this line of code is going to run this built in len function,
calculating the number of letters inside this input that we've given it.
And then it's going to return the result as an output,
which we can now capture inside a variable.
This is the inputs, this is the function
and once this is completed,
the return or whatever it is after the return statement is going to replace this
part of the code.
Then that result or output then gets stored inside of this variable.
So this return keyword is really the most important thing in order to create a
function that has an output,
because everything that comes after it is going to replace where the function
was called. In the next lesson
we'll take a quick look at the return keyword in more detail. Specifically,
let's see what happens
when we have more than one return statement in the same function. All right.
For all of that and more, I'll see you there.