What is a function?
Functions are pretty cool and a great way to set up your code into useful fragments. Doing this will allow for your code to be much more readable, structured and enable you to recycle it so you can save time (a lot of time). The function only runs when you call it. Through a function, you can pass parameters; a function could return data as a result.
How do you write a function?
The structure of a block, what Python makes use of, is written in the format of:
block_head:
block line 1
block line 2
block line 3
block line 4
...
To initiate and define a block, via Python, you use a “def.”
def x():
print("This is a function")
You can even attach an argument to a function:
def x(var1, var2):
print("This is a function with arguments %s"%(var1, var2))
A function can also return a value when you utilize “return.”
def x(y,z):
return y + z
So, how do you call them?
All you need to do is write the function’s name with “(),” and the requirements are within the given brackets. Here is how it will look:
# define the functions
def function1()
print("This is from function 1")
def function2(name, message):
print("Hey, %s! I wish you %s"%(name, message))
def function3(x, y, z):
return x + y + z
# print: This is from function1
function1()
# print: Hey, Bob! I wish you a nice evening!
function2("Bob", "a nice evening!")
# we are going to have the variable hold a value
x = function3(1,5,9)
Exercise
Let’s try an exercise to see if we understand the basics of functions. We are going to try and use a function that is given to us and try and make a program. The requirements will go as followed:
- 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!”
If you get stuck, feel free to look at the solution!
Exercise:
# Modify this function to return a list of strings as defined above
def list_benefits():
pass
# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
pass
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()
Solution:
# 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()
Output:
<script.py> output:
More organized code is a benefit of functions!
More readable code is a benefit of functions!
Easier code reuse is a benefit of functions!
Allowing programmers to share and connect code together is a benefit of functions!
For more information
As always, if you ever do get stuck and require more resources, please check out Python’s official documentation.
Michael is an Information Technology consultant, with a focus on cybersecurity. Every day, Michael strives to learn something new, with an aim to share it with you all!