Arvutiteaduse instituut
  1. Kursused
  2. 2025/26 sügis
  3. Programmeerimise alused (MTAT.03.236)
EN
Logi sisse

Programmeerimise alused 2025/26 sügis

  • Home
  • Grading
  • Links

Before session 3

Functions

Work through topics from the reading materials or watch the videos.

Test

Go to Moodle and take the third test concerning the materials.

Homework

The deadline for homework is Monday, so it would be still good to start with homework before the session.

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

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 = input("Enter your name: ")
foot = float(input("Enter your foot length (cm): "))
shoe = shoe_size(foot)
print("Dear " + name + ", your suitable 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 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:
    x1 = int(input("Enter the first number: "))
    y1 = int(input("Enter the second number: "))
    m1 = maximum(x1, y1)
    print("The input numbers are", str(x1), "and", str(y1) + ". The greatest number is", str(m1) + ".")
    x2 = int(input("Enter the third number: "))
    y2 = int(input("Enter the fourth number: "))
    m2 = maximum(x2, y2)
    print("The input numbers are", str(x2), "and", str(y2) + ". The greatest number is", str(m2) + ".")
    if m2 > m1:
        print("The first greatest number is smaller.")
    else:
        print("The second greatest number is smaller.")
except:
    print("Please enter a number.")

print("The input numbers are 2 and 5. The greatest number is", str(maximum(2,5)) + ".")
print("The input numbers are 4 and 1. The greatest number is", str(maximum(4,1)) + ".")
  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused