Functions
What are Functions?
Functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it and save some time.
How do you call functions in Python?
Good news: We already did that! print() for example is a function, that is built-in to Python. You can call functions by their name, followed by round brackets, which contain data, that you want to give to the function. This is called arguments.
Example:
print("Hello world!")
print is the name of the function and "Hello world!" is a string that we give to this function. Some functions take more than argument. For example, the max() function that returns the largest of its parameters:
max(7, 3, 42, 20)
max() is kind of special, as it can take any amount of arguments.
How do you write functions in Python?
Functions in python are defined using the "def" keyword, followed with the function's name. For example:
def my_function():
print("Hello From My Function!")
"my_function" is the name that you give the function. It's followed by round brackets and a colon. Everything within the function needs to be indented (in our example that is the print).
Arguments can be defined like this:
def my_function_with_args(username, greeting):
print("Hello, %s , From My Function!, I wish you %s"%(username, greeting))
Functions may return a value to the caller, using the keyword "return". For example:
def sum_two_numbers(a, b):
return a + b
Exercise
In this exercise you'll use an existing function, and while adding your own to create a fully functional program.
-
Add a function named
list_benefits()that returns the following list of strings: "More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together" -
Add a function named
build_sentence(info)which receives a single argument containing a string and returns a sentence starting with the given string and ending with the string " is a benefit of functions!" -
Run and see all the functions work together!
# Modify this function to return a list of strings as defined above
def list_benefits():
return []
# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
return ""
def name_the_benefits_of_functions():
list_of_benefits = list_benefits()
for benefit in list_of_benefits:
print(build_sentence(benefit))
name_the_benefits_of_functions()
# Modify this function to return a list of strings as defined above
def list_benefits():
return "More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"
# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
return "%s is a benefit of functions!" % benefit
def name_the_benefits_of_functions():
list_of_benefits = list_benefits()
for benefit in list_of_benefits:
print(build_sentence(benefit))
name_the_benefits_of_functions()
test_output_contains("More organized code is a benefit of functions!")
test_output_contains("More readable code is a benefit of functions!")
test_output_contains("Easier code reuse is a benefit of functions!")
test_output_contains("Allowing programmers to share and connect code together is a benefit of functions!")
success_msg("Nice work!")