Understanding Function in Python
Writing a program is like telling the machine to execute a series of commands. Often, in the whole programming code we are more likely to write the same set of instructions again and again.
Writing the same block of codes again and again is not ideal for programmers as it is more prone to error (typo or incorrect use of variables). To avoid repeating the same lines of codes from time to time, we use Function.
Functions help us to avoid repeating long codes by just referring to a couple words in writing to perform the same instruction.
For example, we would like to write a script to calculate churn rate by subtracting the number of users at the end of the period from the number of users at the beginning of the period and then dividing the result with the number of users at the beginning.
user_beginning = 1560
user_ending = 1300
churn_rate = (user_ending - user_beginning) / user_beginning
print(churn_rate)
[output]: -0.1666666666
user_beginning = 1300
user_ending = 1200
churn_rate = (user_ending - user_beginning) / user_beginning
print(churn_rate)
[output]: -0.076923069
Let’s say we update the two variables to calculate the churn rate for next period. Without the help of the function we will need to write the formula once again to make the calculation as shown above.
To define a function in Python, we need to start with the keyword def. Its syntax is as following:
def function_name(parameter_1, parameter_2):
instruction
There are a couple things you need to know when writing a function based on the syntax above.
- We need to make an indention for the instruction code of the function. Otherwise, Python will not recognize that it is a part of the function.
- Parentheses are important parts of a function. A function may or may not have the parameters. The parameters inside the parentheses are called positional arguments, meaning the first input value will be referred to parameter_1 and the second input value will be parameter_2.
- We can return a value after running a function in order to modify the value later by using return.
Let’s define a function,churn_rate and recalculate the churn rate from the example above.
def churn_rate(user_beginning, user_ending):
churn = (user_ending - user_beginning) / user_beginning
return churn
month_1 = churn_rate(1560, 1300)
print(month_1)
[output]: -0.166666666
month_2 = churn_rate(1300, 1200)
print(month_2)
[output]: -0.076923069
A Default Argument of a function take a constant value when its value is not given when calling the function.
For example, we define a function to calculate the revenue. Any selling price of the item in your store should be at least 1 dollar. Therefore, by default, without declaring any value of a pice, its value is 1.
def revenue(qty, price=1):
return qty * price
Note that default arguments must come at the last after all other official arguments in a function definition.
Often, we may want to return several values from a function. In such cases, you can return those values separated by comma.
def revenue(qty_1, price_1, qty_2, price_2):
revenue_1 = qty_1 * price_1
revenue_2 = qty_2 * price_2
return revenue_1, revenue_2
One last thing which I would like to talk about Function is the concept of Scope. Variables could be defined outside or inside of a function. For those defined outside a function are Global Variables, which means they could be called later, while the variables defined inside a function are called Local Variables, whose value could be used only when the function is called.
a = 5
def f1():
a = 2
print(a)
print(a)
[output]: 5
f1()
[output]: 2
Happy Coding 😎…