[Tutor] recursive function example

ugajin at talktalk.net ugajin at talktalk.net
Wed Dec 11 19:09:35 CET 2013


 
No, not really.
mutl(3, 2) has two arguments
  rest = mult(a, b - 1)  also has two arguments 
but it is passed to value as one argument.
value = a + rest 

But, thanks anyway.

-A

 

 

-----Original Message-----
From: Mark Lawrence <breamoreboy at yahoo.co.uk>
To: tutor at python.org
Sent: Wed, 11 Dec 2013 17:38
Subject: Re: [Tutor] recursive function example


On 10/12/2013 14:48, ugajin at talktalk.net wrote: 
 
[snipped] 
 
As you're clearly struggling here's my attempt at showing you what is happening. 
 
c:\Users\Mark\MyPython>type mytest.py 
level = 0 
 
def mult(a, b): 
    global level 
    level += 1 
    print('level now', level, 'a =', a, 'b =', b) 
    if b == 0: 
        print('immediate return as b == 0') 
        return 0 
    print('call mult again') 
    rest = mult(a, b - 1) 
    print('rest =', rest) 
    value = a + rest 
    print('value =', value, '(a + rest ==',a, '+', rest,')') 
    return value 
mult(3, 2) 
 
c:\Users\Mark\MyPython>mytest.py 
level now 1 a = 3 b = 2 
call mult again 
level now 2 a = 3 b = 1 
call mult again 
level now 3 a = 3 b = 0 
immediate return as b == 0 
rest = 0 
value = 3 (a + rest == 3 + 0 ) 
rest = 3 
value = 6 (a + rest == 3 + 3 ) 
 
c:\Users\Mark\MyPython> 
 
Does this help clarify things for you? 
 
-- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. 
 
Mark Lawrence 
 
_______________________________________________ 
Tutor maillist  -  Tutor at python.org 
To unsubscribe or change subscription options: 
https://mail.python.org/mailman/listinfo/tutor 

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131211/f2ece14a/attachment.html>


More information about the Tutor mailing list