Great, you can now create functions, call them and
add parameters to make them more dynamic. And now
what you can also do is to make a function return
a value. Okay, so this functions, say_hello is
just a function that does an action, it doesn't
return anything. Now I'm going to create a new
function. So as you can see, when you just press
enter here, I still have the indentation. So what
I'm going to do, I'm going to remove that
indentation, and do def again. So let's say I'm
going to create a function double_number, and say
a just number. So you can create as many
functions as you want, you just need to put them
one after the other. Okay, so now I'm going to
create a function with one parameter, and the name
of the function is double_number. And I simply want
to return the double for this number. So I'm
still going to compute the result, which is the
double of that number. So I'm going to create a
variable result is equal to number times two. And
then what I'm going to do, I'm going to use the
return statement, return result. So what this
function will do when it's called? So first it is
going to receive a parameter number, it's going to
compute the double of this number and return the
result. So when you call the function, you can get
the double of the number you sent. Just before we
continue, one thing I'm going to do here is that
so we create a variable and we just return that
value. So basically, we are not going to use that
we only return that variable. So what we can do
instead of result is equal to number times two and
return result, we can just return number times
two and remove that. So this is going to return
directly the correct value. And now well, I can
call this function, so I'm just going to put a
comment here. Okay, so we don't call those so this
is not a problem, you could call this and call
this, that's not a problem. Here, I just want to
call that function and not this one. So we just
have less lines, okay, in the output. So what I
can do, I can do double number, I can use the auto
completion, with let's say, three, okay. And I'm
going, of course, to store that. So let's say
number is equal to double_number(3), and then
print number. So when you create a function that
returns something with the return statement, you
can, when you call the function, you can actually
assign the value, you get here to a variable. And
then you can print of course, the variable, or you
can do whatever you want with the variable. So I
just ran the program, and you can see here we have six, so
the double of three is six, so the number variable
here contains the value six. Alright, so you can
see two types of functions here. So those who are
just going to do an action and don't return
anything, and those who compute or maybe get a
value from some way and then return that value.
Now that you can return any kind of value you
want, here, I return an integer number, but you
can return a float, a Boolean, string, list of
whatever you want, well just return what you want
from a function. But just make sure that when you
call the function, you actually know what kind of
data type you're going to get. Okay, and one thing
you can also do is you can call a function inside
another function. And those you can really make
functions as building blocks that allow you to
create even more building blocks. Let's see that
with an example. def print_double_number. So
we've number and I'm going to do results is equal
to double_number of the number we get here,
and then print, result or maybe make it better.
So Double of plus, the number cast it as a
string plus is and plus the result also casted
as a string. So when you call this function, what
it's going to do is, going to call the double_number function.
Okay, so from the function you
call another function, and you can see this
parameter I got here, I'm going to pass it also to
the double_number function. And then I can print
some stuff with the parameter and the result I
got. And I'm going to call this print_double_number.
I'm going to remove that.
I'm going to remove that and I'm simply going to
run. And you can see double of three is six.
Okay. It's all happening in that function. So here
you have a function that just does an action and
that's it. Here a function that returns a number.
And here a function that does an action but inside
you actually call another function and you can
create another function that is going to call that
one, etc, etc.