## Before session 4

### Functions

Watch the videos about defining and using functions in Python:

*Slides** in English*

*Textbook** in English *

### Quiz

Go to Moodle and solve the quiz about functions. You can attempt the quiz several times, the best attempt counts.

### Examples

This week we are going to get an insight into functions and definitions (*def*). Functions allow to perform tasks that are similar to each other and differ only by values of parameters. So there is no need to rewrite essentially the same algorithm many times, we can just call the function with right parameters.

#### Example 1

Rewrite the shoe size program using a function called *shoe_size* that takes foot length as its parameter and returns shoe size.

def shoe_size(length): size = 1.5 * (length + 1.5) return size name = input("Enter your name: ") foot = float(input("Enter your foot length (cm): ")) shoe = shoe_size(foot) print("Dear " + name + ", your shoe size is "+str(round(shoe))) print("Shrek's shoe size is", round(shoe_size(42.3)))

#### Example 2

The following example shows the function that takes two parameters (two numbers) and returns the largest of them. In the example, the function is called twice; therefore, the results are stored in two variables. After that the smallest of two largest is found. To show the effect of the function, we test it on different inputs.

def maximum(a, b): if a < b: return b else: return a try: x1 = int(input("Enter first number: ")) y1 = int(input("Enter second number: ")) m1 = maximum(x1, y1) print("Input numbers are", str(x1), "and", str(y1) + ". The largest is", str(m1) + ".") x2 = int(input("Enter third number: ")) y2 = int(input("Enter fourth number: ")) m2 = maximum(x2, y2) print("Input numbers are", str(x2), "and", str(y2) + ". The largest is", str(m2) + ".") if m2 > m1: print("The largest number in first pair is smaller.") else: print("The largest number in second pair is smaller.") except: print("Please enter a number.") print("Input numbers are 2 and 5. The largest is", str(maximum(2, 5)) + ".") print("Input numbers are 4 and 1. The largest is", str(maximum(4, 1)) + ".")

### Exercises

#### 1. Bakery cash register

A bakery produces three types of cakes: chocolate cakes (0.05 â‚¬ / cm^{2}), strawberry cakes (0.04 â‚¬ / cm^{2}), and Napoleon cakes (0.08 â‚¬ / cm^{2}). Write a function called *cake_price*, which takes cake name and cake size as parameters and returns the price of the cake in euros, rounded to two decimal digits. Chocolate cake and strawberry cake are round and their sizes are entered as radius. Napoleon cake is square-shaped and its size is entered as side length. If cake type is something else, then the function should return -1.

>>> cake_price('Napoleon cake', 10) 8.0 >>> cake_price('chocolate cake', 5) 3.93

Cake names are 'chocolate cake', 'strawberry cake' or 'Napoleon cake'. Size is given in centimeters.

#### 2. Einstein's Theory of Special Relativity

If two bodies are moving in the same direction, where the speed of the first body with respect to the observer is *u* and the speed of the second body with respect to the first body is *v*, then according to the special theory of relativity, the speed of the first body with respect to the observer is calculated form the formula

{$$\frac{u+v}{1+\frac{u\cdot v}{c^2}},$$}

where *c* is the speed of light (*299792,458 km/s*). This expression is called the sum of speeds *u* and *v*.

Create a function called *sum*, whose parameters are speeds *u* and *v*, and which returns the sum of these speeds according to special theory of relativity.

>>> u = 100000 >>> v = 200000 >>> sum(u, v) 245392.74884785622

Use this function to calculate the sum of speeds of four bodies that move in the same direction. The program should prompts for four speeds and print out their sum.

The speed of the first body with respect to the observer is:100000The speed of the second body with respect to the first is:150000The speed of the third body with respect to the second is:200000The speed of the fourth body with respect to the third is:250000Sum of speeds is 297993.41836837644 km/s

#### 3. Price difference

Write a function *price_difference*, which calculates the difference of total cost on the installment plan and the initial price of the product. The function should take three parameters in the following order:

- initial price of the product
- monthly payment
- number of months on the installment plan

Example of function call:

>>>price_difference(200,10,30) 100 >>>price_difference(200,20,12) 40

Then write a program that asks the user for the initial price of the product and parameters of two different installment plans. By calling the function *price_difference*, the program compares the plans and outputs the plan which is better for the user.

Example:

What is the price of the product?200What is the monthly payment on the first installment plan?10How many months does the first installment plan last?30What is the monthly payment on the second installment plan?20How many months does the second installment plan last?12The second installment plan is better!

You can assume that all numbers are integers.

### Submit your solutions

Submit your solutions of exercises 1, 2, 3 to the autograder in Moodle under the names *home1.py*, *home2.py*, *home3.py*, respectively.