Functions are programming constructs that helps us package common operations. In addition they make our code that much more readable.
They have a format that looks like this:
def first_function():
pass
output = first_function()
OR
def first_function():
some_string = "An example string"
return some_string
output = first_function()
Basically we start by telling python that we are about to define a function by using the keyword def and then give the name of the function followed by round brackets with any arguments and then the body of the function.
A function in python must have a body. If none is available, say if you are still sketching out the overall system, then use the keyword pass
If you don't explicitly return an output. Python will return a None object. See the reference.
Note that Method is another way of referring to Function. This terminology is more common in OOP conversations.
##Step 1
Let us implement this requirement
As a user I want to see welcome message from fairmont when I run the welcome_message.py script
Let us start by creating our lesson folder
mkdir 6lesson
cd 6lesson
touch welcome_message.py
Now open the welcome_message.py in the 6lesson folder and type in the following code.
def welcome_message():
message = "Welcome guest to Fairmont hotels. It is a pleasure to serve you"
return message
output = welcome_message()
print(output)
Run the code from the bash terminal
python3 welcome_message.py
What did you see on the screen?
###Assignment
Can you make the script print out a welcome message for you instead of generic guest
##Step 2
Now that we are able to print out a string, let us make it more useful by having it accept an argument. An argument is simply a value that you pass to a function or method.
Suppose the requirement is now.
As a guest I want the script welcome_message to first ask for my name and then give me a personalized message
Modify your welcome script so that it now looks like this.
def welcome_message(name):
message = "Welcome {} to Fairmont hotels. It is a pleasure to serve you".format(name)
return message
name = input("What is your name?")
output = welcome_message(name)
print(output)
Run the script.
python3 welcome_message.py
What do you now see as the output?
##Step 3
Now that we are able to work with strings, how would a function help us with numbers.
Suppose we now have a requirement.
As a user I want the script area_triangle.py to first ask me for a base and height and then give me an area
We can now create a new file called area_triangle.py.
touch area_triangle.py
And then type in the following code into your python file
def area_triangle(base,height):
int_base = int(base)
int_height = int(height)
area = 0.5 * int_base * int_height
return area
base = input("What is the base of your triangle?:")
height = input ("What is the height of your triangle?:")
area = area_triangle(base,height)
print(area)
You notice that we moved the operations necessary to clean up the data into the function itself.
What was the result?
##Assignment
Create two more scripts, area of rectangle and area of circle. When the files are run the area of rectangle should ask for a base and height and then give the area. The area of a circle should ask for radius and then give the area
##References