Grokking Algorithms


def greet2(name): print “how are you, “ + name + “?” def



Download 6,4 Mb.
Pdf ko'rish
bet28/120
Sana21.12.2022
Hajmi6,4 Mb.
#893167
1   ...   24   25   26   27   28   29   30   31   ...   120
Bog'liq
Grokking Algorithms An Illustrated Guide for Programmers and Other

def
greet2(name):
print “how are you, “ + name + “?”
def
bye():
print “ok bye!”
Let’s walk through what happens when you call a function.
Suppose you call 
greet(“maggie”)
. First, your computer allocates a box 
of memory for that function call.
Now let’s use the memory. he variable 
name
is set to “maggie”. hat 
needs to be saved in memory.
Note
print
is a function in Python, but to make things easier for this example,
we’re pretending it isn’t. Just play along.


44
Chapter 3
 
 
I
 
 
Recursion
Every time you make a function call, your computer saves the values 
for all the variables for that call in memory like this. Next, you print
hello, maggie!
hen you call 
greet2(“maggie”). 
Again, your 
computer allocates a box of memory for this function call.
Your computer is using a stack for these boxes. he second box is added 
on top of the irst one. You print 
how are you, maggie?
hen you 
return from the function call. When this happens, the box on top of the 
stack gets popped of. 
Now the topmost box on the stack is for the 
greet
function, which 
means you returned back to the 
greet
function. When you called the 
greet2
function, the 
greet
function was
partially completed. 
his is 
the big idea behind this section: 
when you call a function from another 
function, the calling function is paused in a partially completed state. 
All 
the values of the variables for that function are still stored in memory. 
Now that you’re done with the 
greet2
function, you’re back to the 
greet
function, and you pick up where you let of. First you print 
getting ready to say bye…
. You call the 
bye
function.


45
The stack
A box for that function is added to the top of the stac
k. hen you print 
ok bye!
and return from the function call.
And you’re back to the
greet
function. here’s nothing else to be done, 
so you return from the 
greet
function too. his stack, used to save the 
variables for multiple functions, is called the 
call stack
.

Download 6,4 Mb.

Do'stlaringiz bilan baham:
1   ...   24   25   26   27   28   29   30   31   ...   120




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish