Welcome back. Now, let's talk about
parameters and arguments.
See a parameter is basically an input
that we can add to a function in order to customize its behavior
in this case right now, in the previous example,
the name of the function, which is great, it didn't have any parameters at all.
It's basically empty.
That's why
when we said OK, print and then simply hello world and then we call the function.
All it simply did was just simply say hello world.
But what if we now want to add a particular parameter inside in this case right now,
I'm going to come in here right now and say name.
OK.
So the parameter right here is called name. And now
I want my function to say
hello and then the name that will be provided.
So
I'm going to come over here right now. Let's use the F string.
And now I'm going to add my curly braces, color brackets rather
and call it name.
OK.
So now check this out
when I'm calling my function right now, I can add what we call the argument.
When you're passing a value to the function,
it's going to become an argument.
Please note the difference between parameters and arguments
up here. When we're first defining the function,
we're gonna use the parameter. The parameter is
basically all saying, hey,
we're providing you with this input and I'm going to use
this input to customize how you the function grid will behave.
However,
once the function has been created,
we've told what the function is supposed to do in this case right now.
Hello and then name the parameter.
When we call that function.
When we want to provide a value, it's going to become what we call an argument.
So in this case, right now,
if I wanted to greet, let's say Alice, for example, right,
Alice right now is the argument. And if I was to run the program,
there you go. It says hello, Alice. What if I wanted to greet? Let's say Bob,
right,
Bob,
let's go ahead and run the program and there it is. Hello, Bob.
These are
parameters and arguments.
However,
we can also have multiple parameters
in our function.
So far right now, we've only been working with one single parameter which is name.
What if
we wanted to create a function that will add two numbers together?
How are we going to do that? Well,
we can start off by defining the function name. So I'm going to call this one. Add
numbers,
add numbers will be the name of the function.
And now inside
we're going to provide two parameters which are going to be A and B
colon.
OK?
Now
return
A plus B,
we haven't talked about the return function
or return statement yet.
What return does here is that it's simply
going to return the value
of a particular operation. In this case right now
is going to return
the values of A and B when they are added together. That's what the return does. So now
I'm gonna come in here
and say uh results,
OK? Results
is going to be equal to
add
numbers.
OK? And now inside
I can add the two numbers. I want to add, let's say five and six.
And now all I will have to do
is to simply print
the result.
And there it is.
If I run my function,
there you go. 11,
you see how this works. First of all, we created a function
but isn't the defined function called add numbers. OK?
That's the name of a function.
And then we said, OK, our function is going to receive two parameters A and B.
And now with the return, we're basically defining what the function is going to do.
It's going to return the value of A plus B.
Now,
all we need to do is to create a variable
that will store the value of our function.
So now the variable here is called the result, which is equal to add number.
The name of the function call in right now
with the two values which are five and six.
And now we simply printed out results which is equal to 11.
That's exactly how parameters arguments and
the return function also works.
By the way before
I run up the video,
just a very quick word on the return statement.
It's used for two main purposes. OK?
The first purpose is typically to exit a function
so you can use it to stop function execution.
Second is to return an actual value. So
as an example,
let me just show you real quick if I set the fine and then greet,
OK,
let me add
empty parameters in here.
And I said print, hello
and then I said return,
all right. But then after return on another line, I also say print
and then let's say, uh
let me say
my coats.
Hello,
Jack.
You can even see right now that within our replete,
it's
the text itself is kind of faded out,
meaning that Python will not execute this particular
line. So if I was to run,
you can see right now it does absolutely nothing at all.
But that's because I haven't called the function yet. So
let me come over here and say greets and then
brackets and there you go. So
if I was to run the function right now, you can see all it says is going to be hello.
Why? Because return right here signifies that, hey,
stop the execution of the function. It shouldn't go beyond this particular line
and that's why we only have hello. However,
if I was to remove
the return statement
and I run the program again, you can see right now it says hello and then hello Jack.
So this is another use of the return statement.
You can use it to stop function execution or like I showed you earlier,
you can use the return statement to return a particular value.
Thank you for watching the video. I will see you in the next class.