## Before session 3

### Functions

Watch the video:

*Slides** in English and in Russian*

*Textbook** in English *

*Helpful link** in Russian_2.pdf *

### Test

Go to Moodle and take the third test concerning the video lecture.

### Exercises

This week we are going to get an insight into functions and definitions (*def*).

#### Example 1

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

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

#### Example 2

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

def maximum(a, b): if a < b: return b else: return a try: x = int(raw_input('Enter first number: ')) y = int(raw_input('Enter second number: ')) m = maximum(x, y) print "The input numbers are", x, "and", y, "thus the greatest number is", m except: print 'Please enter a number' print "The input numbers are 2 and 5 thus the greatest number is", maximum(2,5) print "The input numbers are 4 and 1 thus the greatest number is", maximum(4,1)

#### Exercise 1. Number of days in months

Rewrite the number of days in months program using a function called *number_of_days* that takes a number of the month as its parameter and returns the number of days in that month.

Example of function call:

>>>number_of_days(3) 31 >>>number_of_days(9) 30

Test the program on various input values.

#### Exercise 2. Price difference

Write a function called *price_difference* that calculates the difference between the price on the installment plan and the initial price of the product. The function should take three parameters:

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

Example of the function call:

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

Upgrade the program so that it asks the user for the initial price of the product and two different installment plans. The program compares the plans and outputs the plan which is the best for the user.

Example:

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

Hint

Save in a variable the price difference which is returned by the function. To call the function twice, you will need two different variables. You can then compare these two values and use *if-else* to choose the best installment plan.

**Go to Moodle and put your solutions into Homework 3**.