AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Contents · Computing

Subprograms: functions and procedures

Lessons 47–48 · 2 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
48

Practical lesson: functions

Textbook: p. 95
GoalIn the practical lesson, solves tasks with functions and procedures: finds the greatest number, prints even numbers, and calls a function inside another function.
New words
calling a function: writing its name with the values in brackets · funksiyani chaqirishnested call: the result of one call is used as an argument of another · ichma-ich chaqiruvprocedure: a function that prints but returns no value · protsedurabuilt-in name: names like max, len, print that should not be used for your own functions · o‘rnatilgan nom
Explanation

When working with functions first say the job of the function in one sentence: “returns the larger of two numbers”, “prints the even numbers up to n”. The first returns a value (return), so its result can be used in another expression: katta(katta(a, b), c) is the largest of three numbers. The second prints the result at once and returns no value, that is, it is a procedure. Choose a name by its meaning and avoid built-in names like max and len; this is why we named the function that finds the larger number katta. After writing a function test it by calling it with at least two different values. The declaration of a function must come before its call, otherwise a NameError appears.

💡 Where will I use this?
In real life

In a class project several pupils each needed to check whether a score reached the pass mark (at least 50). Instead of everyone writing their own check, one pupil wrote a function once and the others called it. When a wrong sign was found, it was fixed in one place and everyone’s results were corrected.

Who uses it

Programmers, analysts and testers reuse ready functions to save time.

Try it today

Choose a small job such as “returns the number of days in a given number of weeks”. Write the job in one sentence and add two input–output examples on paper.

Worked examples
Python
1def katta(a, b):
2    if a > b:
3        return a
4    else:
5        return b
6a = int(input())
7b = int(input())
8c = int(input())
9print(katta(katta(a, b), c))
Output
31
The largest of three numbers – code above. For 14, 31, 22 it prints 31.
Python
1def juftlar(n):
2    for i in range(2, n + 1, 2):
3        print(i, end=" ")
4juftlar(10)
Output
2 4 6 8 10
An even-numbers procedure – code above. juftlar(10) prints 2 4 6 8 10 on the screen and returns no value.
Class activity

“Function library”: the group writes 3 small functions (the smaller, square, digit sum) and collects them in one file; another group calls and uses them in its own task.

Practice
1
Write a program using a function kichik(a, b) to find the smallest of three numbers (4 for 9, 4, 6).
2
Write a function ortacha(a, b, c) that returns the mean of three numbers and print ortacha(4, 5, 9) (6.0).
3
What does juftlar(7) print on the screen?
4
Why is it better not to name our own function max?
Story of the Week · The Khwarizmi WayA quality problem and the processClose the week: all of this week’s subjects in one storyRead →