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

Local and global variables, the GCD (EKUB) function

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

Functions and variables

Textbook: pp. 96–98
GoalKnows the difference between local and global variables, uses the global keyword, and understands that if a parameter changes inside a function the outer variable does not change.
New words
local variable: a variable created inside a function; it exists only there · lokal o‘zgaruvchiglobal variable: a variable created outside functions; it can be read everywhere · global o‘zgaruvchiglobal: the keyword that lets a function change a global variable · globalscope: the part of the program where a name can be used · ko‘rinish doirasi
Explanation

A variable declared inside a function is local and exists only in that function: when the function ends it disappears, and if it is used outside a NameError appears. A variable declared outside functions (in the main program) is global and can be read inside a function. To change the value of a global variable in a function, global name must be written at the start of the function; otherwise Python considers the name local and b = b + 1 gives an UnboundLocalError. A parameter is also a local variable: if k = 7 is passed to a function, doubling the parameter inside does not change the outer k. Global variables can make a program confusing, so it is better, where possible, to pass values by parameters and return the result with return. Local variables protect a value from being spoiled by accident elsewhere.

Local variableGlobal variable
Declaredinside a functionoutside functions
Existsonly in that functioncan be read everywhere
💡 Where will I use this?
In real life

A pupil’s notebook had a rough page and a clean page. What is on the rough page belongs only to that page, while what is on the clean page may be needed in many places. Local and global variables are separated in the same way.

Who uses it

Programmers, project managers and system designers decide which value stays inside and which is shared.

Try it today

Write two lists: “needed only today” and “needed every day”. Then explain in one sentence which is like local and which like global.

Worked examples
Python
1def f():
2    t = 7
3    print(t)
4f()
5print(t)
Output
7
NameError
A local variable – code above. f() prints 7 and then the last print(t) gives a NameError, because t exists only inside f.
Python
1hisob = 0
2def oshir():
3    global hisob
4    hisob = hisob + 1
5oshir()
6oshir()
7print(hisob)
8
9def f(n):
10    n = n * 2
11    print(n)
12k = 7
13f(k)
14print(k)
Output
2
14
7
global and a parameter – the first program above. Thanks to global hisob the function changes the outer hisob; after two calls 2 is printed. A parameter – the second program above. It is 14 inside, while k stays 7 outside.
Class activity

“The circle boundary”: the classroom is a “function” – a circle of desks; a pupil inside uses the card in the hand (local) only in this circle, while the writing on the board (global) is seen by everybody. The teacher asks questions to find who can see what.

Practice
1
Which error appears if b = b + 1 is done in a function without writing global?
2
What does this program print?
Python
1x = 1
2def f():
3    x = 2
4    print(x)
5f()
6print(x)
3
Write a function oshir(x) that increases b = 5 by 1 without global and return the result to b.
4
Why is it advised to use global variables as little as possible?
Story of the Week · The Khwarizmi WayGiving part of the profitClose the week: all of this week’s subjects in one storyRead →