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
47

Subprograms: functions and procedures

Textbook: pp. 91–94
GoalKnows the concepts of a subprogram, a function and a procedure, writes and calls a function with parameters using def and return, and explains recursion and its stopping condition.
New words
function: a named subprogram that takes parameters and may return a result · funksiyaparameter: a name in the brackets of def that receives the value passed when the function is called · parametrreturn: gives the result back to the place where the function was called · returnrecursion: a function that calls itself · rekursiya
Explanation

When the same job is repeated in several places in a program, it is handy to move it into a subprogram – a function: the code is written once and a mistake is fixed in one place. A function is declared with def name(parameters): and its body is written with an indent of 4 spaces; it works only when it is called: name(values). return is used to give the result back to the place of the call; without return the function returns nothing (None) – in the textbook such a subprogram is called a procedure. A function name should not be the same as built-in names such as max, len, print, otherwise they are “hidden”. If a function calls itself it is recursion: for example n! = n · (n − 1)!, 0! = 1. Recursion needs a stopping condition (if n == 0: return 1), otherwise the calls never stop and a RecursionError appears.

PartJob
def name(parameters):declares the function
returngives the result back to the place of the call
name(values)calls the function
💡 Where will I use this?
In real life

A school club wrote the same invitation to classmates each time. Then they made it one template where only the name changes. A function works the same way: written once and called many times.

Who uses it

Programmers, event organisers and office workers write a repeated job once as a template or function.

Try it today

Pick a small calculation you repeat, such as changing minutes into hours, and write it on paper as a rule “f(x) = …”. Test it with two different inputs.

Worked examples
Python
1def kvadrat(x):
2    return x * x
3print(kvadrat(7))
4
5def salom(ism):
6    print("Salom,", ism + "!")
7salom("Lola")
Output
49
Salom, Lola!
A function and a procedure – the first program above. kvadrat(7) returns 49. The second program is above: salom("Lola") prints Salom, Lola! on the screen and returns nothing.
Python
1def fakt(n):
2    if n == 0:
3        return 1
4    return n * fakt(n - 1)
5print(fakt(5))
6
7def salom():
8    print("Salom")
9x = salom()
10print(x)
Output
120
Salom
None
Recursion – the first program above. fakt(5) = 5 · fakt(4) = … = 5 · 4 · 3 · 2 · 1 · fakt(0), fakt(0) = 1, the result is 120. A subprogram that returns no value – the second program above. None goes into x.
Class activity

“A function is a machine”: the class plays a function as a “machine”: one pupil gives a parameter (for example 7), the “machine” pupil follows the rule (square) and returns the result (49); then the rules are changed.

Practice
1
What does the function f(x) = 2 * x + 1 return for f(4)?
2
Write a function yuza(a, b) that returns the product of two numbers and print yuza(6, 7).
3
What is the recursive fakt(4) equal to?
4
Name two advantages of using functions.
Story of the Week · The Khwarizmi WayA quality problem and the processClose the week: all of this week’s subjects in one storyRead →