Posted on

Learn Python Episode #16: Adding Arguments to a Function

Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/

Enroll in The Complete Python Course on Udemy!
https://www.udemy.com/python-complete/?couponCode=PYTHONWP

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.

def my_function(str1, str2):
print(str1)
print(str2)

my_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.

def my_function(str1, str2):
print(str1)
print(str2)

my_function("This is argument 1", "This is the second argument which is also a string.")

Now when we call my_function it will print out the two strings. Let’s call the function again and print out two different strings.

def my_function(str1, str2):
print(str1)
print(str2)

my_function("This is argument 1", "This is the second argument which is also a string.")
my_function("Stringy", "Hello World")

As you can see, Python will print out all four strings.

Web – https://josephdelgadillo.com/
Subscribe – https://goo.gl/tkaGgy
Follow for Updates – https://steemit.com/@jo3potato