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 in to 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 of 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 3 * 2, 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 equals 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 six.
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, out
comes 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 starting file for today's lessons.
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 called
f_name,
so first name, and another called 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 get passed in,
so it could be something like this all lowercase,
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 StackOverflow.
"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 f_name and l_name, so that they end up being
both in title case.
Pause the video and give that a go.
All right.
So we want to go ahead and take this f_name,
and then write a dot,
and then write title (.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 just angela, all in lowercase,
and then the second one is going to be ANGELA,
all in uppercase.
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 f-string
to take the formatted f_name, and then add a space, and then print out the formatted l_name 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 case, my last name gets converted to title case,
and then that result is stored in formatted f_name.
And this result becomes stored in this l_name, 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 have 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 f_name and l_name.
Now the formatted_f_name becomes the title case 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, I seem 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 input.
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 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.
Now students often ask me, well, what is the difference then between a print statement and the output?
Because it seems like we could use either to get to print to the output area.
Well, here is the explanation
if you also have this question in mind right now. Let's consider that we create a different function,
and this function doesn't actually print its output at all.
So let's define a function called function_1(),
and it takes some text as input.
And all it does is it outputs the original text, but it adds the original text together.
So this way if I had the word for example, "hello", then the output would become "hellohello" like an
echo.
So now if I go ahead and create another function, let's call it function_2(),
and it also takes some text as input,
well in this case, this function_2() is going to return the text, but it's going to return it in title
case.
So we've already seen this before and we know how this works.
It's going to take whatever input it is here,
make the first letter capitalized
and the rest of the letters lowercase.
So neither of these functions have a print statement.
So let's go ahead and call our first function and pass
in a word let's say, "hello".
Now we can save this to an output variable,
and then go ahead and print it.
Now you'll see I have that echo thing that I told you about earlier.
But now here's the key part,
what if, if we take the output of this function_1 and we use it as an input into function_2, how
would we do that?
Well, we know that the output of function_1, this is in fact a string,
and it is a piece of text.
So we can then put it into function2 as the input.
And we would do it like this function2(), and then brackets,
and so now the output of function_1 becomes the input for function_2,
and then that input is then title cased.
So now if we run our code you'll see that not only do we have the Hellohello functionality being carried
out, we've also got the final text capitalized at the beginning.
So that's pretty interesting right?
We're only able to do this because we understand how the return function works.
And we've got two functions that have outputs.
And we've effectively chained the output of one function into the input of another one.
Now this might mess with your head a little bit, so don't worry.
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.