Grokking Algorithms


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



Download 24,82 Mb.
Pdf ko'rish
bet29/122
Sana22.07.2022
Hajmi24,82 Mb.
#839971
1   ...   25   26   27   28   29   30   31   32   ...   122
Bog'liq
grokking-algorithms-illustrated-programmers-curious

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. The variable 
name
is set to “maggie”. That 
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!
Then 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. The second box is added 
on top of the first one. You print 
how are you, maggie?
Then you 
return from the function call. When this happens, the box on top of the 
stack gets popped off. 
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. 
This 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 left off. 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 stack. Then you print 
ok bye!
and return from the function call.
And you’re back to the
greet
function. There’s nothing else to be done, 
so you return from the 
greet
function too. This stack, used to save the 
variables for multiple functions, is called the 
call stack
.

Download 24,82 Mb.

Do'stlaringiz bilan baham:
1   ...   25   26   27   28   29   30   31   32   ...   122




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