In this video we're going to expand on functions, and we're going to learn how to implement arguments into our functions. The following function is completely static.
def my_function(): print("This is a function!") print("A second string.")
my_function()
We could call this function 10 times and each time it's going to print out the same strings. So, how can we make our functions more dynamic? The way to do this is by adding arguments, and arguments go in the parenthesis at the end of the function name. So, let's add two arguments in to our function.
Now if we try to run this we would get an error. Python is looking for the values str1 and str2 and they are undefined. Let's go ahead and define them.
Alright guys, welcome back. In this video we're going to expand on this function
here and we're actually going to implement arguments into the function. So what
this means that this is very static right now, nothing would change this even if we
wanted to change it. I could call this function let's say 10 times and each
time it's going to print out very static strings. So how we can change this to
make it more dynamic is by using arguments and the arguments again go in
the brackets here at the end of the function name, and what you're doing is
creating a variable so we don't even need to know the value that's being
passed in we just let's decide to use two arguments here in this function, and
each one is going to be used in its own print statement. So we're going to name
it "str1, and str2" there and this basically we've abbreviated string 1 and
string 2. It also tells us right now it's not being used.
You're going to find PyCharm pretty smart. So what we're going to do is
change this line of code to print out string 1, we're going to use this print
statement to print out
string 2. Let's go ahead and save it and run it.
We're going to get an error and the reason is because we're calling this
function without passing anything into it, and it actually tells us right here
parameter string 1 unfilled. So it knows that this function is looking for two
values here separated by a comma, one of them is string one and one of them is
string two, we haven't passed anything into it so it's calling this function
and immediately the function is freaking out saying I can't find this information.
So what we need to do is pass in two strings here and how we do that is
similar to how it looks in here except each one is going to be the value and
not the variable name. So, the first one is going to be called "This is
argument 1",
"This is the second argument which is also a string.")
Now if we save, go-ahead run,
you're going to see that this is now printing out string 1 as defined right
here, and string 2 as defined right here. So, why would we need to do this? Well,
let's say we're always going to want to print out two things.
Let's go ahead and call the function again, but this time we're going to pass
in different strings. Let's fall back to the good ole hello world. Now we're going to
save this and what's gonna happen is it's going to print out four lines. It's
going to print out "This is argument 1", "This is the second argument which is
also a string." being called from the first call of that function, and then "Stringy" , "Hello World"
from the second call that function and so that's how to use arguments. Now in
the next video we're going to be discussing keyword arguments and what
those are.