On To Functions

Why do we need functions?

creative commons licensed (BY) flickr photo by kevin dooley: http://flickr.com/photos/pagedooley/8435953365creative commons licensed (BY) flickr photo by kevin dooley: http://flickr.com/photos/pagedooley/8435953365

 

In Germany, we have a so called „Pfandsystem“ for plastic bottles. Every time you buy a plastic bottle, you pay about 25 cents as a deposit and when you return the plastic bottle, you get your deposit back and the bottle is being recycled and reused again. At first glance, this approach sounds like the perfect solution for recycling: less plastic bottles on the street and a clean environment for our next generations. However, it turned out that only 30 percent of the water and juice bottles that were sold were for multi-use in 2017. So even though the approach of recycling was for a good purpose, the system was not well established. It was a major issue, for instance, that the public could not distinguish between the bottles for multi-use and those for single-use.

So what can we learn from it?

As a programmer, you need to put a lot of effort into establishing a stable system. It is very important to create a code that is easy to read and understandable. Because if not, we have seen it with the bottles, you can not get the best out of your system. Programmers or even yourself won’t be able to work on the code later on, because most probably you would not understand what you did before. So defining functions helps in order to get structure to your code making it readable and reusable. That means once you’ve made that pre-work, you are profiting from it in the long-run. Additionally, having functions defined, it is a lot easier for you to write your code, since you can use a definition as a key instead of large functions.

 

What was our task for this assignment?

You will go back and do WSQ01 – Fun with Numbers again.

But this time, write a function for each calculation. Each function should define two parameters (in this example of type int) and return the correct value as an integer as well.

You main program needs to ask the user for the input and then call each function to calculate the answer for each of the parts.

 

How did I solved it with my Mac?

Program that I used: PyCharm CE

 

  1. First of all, I wrote down all of the functions that are required:
def addition(x,y):
    return x + y

def subtraction(x,y):
    return x - y

def multiply(x,y):
    return x * y

def divide(x,y):
    return x / y

def remainder(x,y):
    return x % y

2. Then, I assigned the computer to ask for the two integer numbers:
print("Insert two integer numbers. ")
x =int(input("Enter the first number: "))
y =int (input("Enter the second number: "))

3. In order to simplify the output of the result, I created values for the respective functions:

# RESULTS FOR PRINTING
r_add = str(addition(x,y))
r_sub = str(subtraction(x,y))
r_multi = str(multiply(x,y))
r_divi = str(divide(x,y))
r_rem = str(remainder(x,y))

4. At the end, I assigned the system to print out the results. 

print("The result of", x , "+", y, "is", r_add + ".")
print("The result of", x , "-", y, "is", r_sub + ".")
print("The result of", x , "*", y, "is", r_multi + ".")
print("The result of", x , "/", y, "is", r_divi + ".")
print("The result of", x , "%", y, "is", r_rem + ".")

5. The whole code looks like the following:

def addition(x,y):
    return x + y

def subtraction(x,y):
    return x - y

def multiply(x,y):
    return x * y

def divide(x,y):
    return x / y

def remainder(x,y):
    return x % y


print("Insert two integer numbers. ")
x =int(input("Enter the first number: "))
y =int (input("Enter the second number: "))


# RESULTS FOR PRINTING
r_add = str(addition(x,y))
r_sub = str(subtraction(x,y))
r_multi = str(multiply(x,y))
r_divi = str(divide(x,y))
r_rem = str(remainder(x,y))


print("The result of", x , "+", y, "is", r_add + ".")
print("The result of", x , "-", y, "is", r_sub + ".")
print("The result of", x , "*", y, "is", r_multi + ".")
print("The result of", x , "/", y, "is", r_divi + ".")
print("The result of", x , "%", y, "is", r_rem + ".")

6. This is how the program’s run looks like:

 

Hinterlasse einen Kommentar