Example of defining and calling functions in Python
# A function without any input parameters
# that does not return data.
def greeting():
print("Hola")
# A function with a single input parameter
# we expect to be a string that does not
# return data.
def person_greeting(name):
print("Welcome, " + name)
# A function to calculate the distance between two points.
# We expect four integers total to represent the x, y values
# of each point and we return the calculated distance.
def calc_distance(point1_x, point1_y, point2_x, point2_y):
# distance formula
√((x2-x1)^2 + (y2-y1)^2)
x = (point2_x - point1_x) ** 2
y = (point2_y - point1_y) ** 2
# Return the distance
return (x + y) ** 0.5
# Function calls
greeting()
person_greeting("Sam")
person_greeting("Sally")
# Variables for each of the x, y coordinates
x1 = 10
y1 = 30
x2 = 5
y2 = 7
# Holds the result of the distance calculation
point_distance = calc_distance(x1, y1, x2, y2)
# Display the value to the screen
print(point_distance)