Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie definining functions question

Status
Not open for further replies.

Marv73

Programmer
Oct 17, 2000
2
0
0
US
Ok, I'm going through the documentation (the informal introduction to 2.0 part) and I'm a little confused about defining functions. It gives this example of the Fibonacci sequence:

>>> def fib(n): # write Fibonacci series up to n
... "Print a Fibonacci series up to n"
... a, b = 0, 1
... while b < n:
... print b,
... a, b = b, a+b
...
>>> # Now call the function we just defined:
... fib(2000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

Now keep in mind, I'm moving to python from C++

Ok, first of all the Fibonacci function is recursive right? Well I don't see any calls to fib within the function, what's with that? =P

Next, what does &quot;a, b = 0, 1&quot; do? To me it would seem it declares a, assigns b to 0, and then prints one or declares it or something =P

And why is it when I try, &quot;a = 0, b = 0, 1&quot; it says it can't assign to literal? I just typed that in trying t figure out what the a, b = 0, 1 did. [sig][/sig]
 
First, This function is not recursive. All the work is being done in a while loop. The key is &quot;while b < n&quot;.

Second, the variable a is being assigned to 0 and b is assigned to 1 in &quot;a, b = 0, 1&quot;. The same thing is happening in the while loop, &quot;a, b = b, a+b.&quot; In this case, the value of b is assigned to a, then b gets the &quot;old&quot; value of a plus the value of b.

Lets trace this a little and see what is happening. We'll start with the while loop:
while b < n: [tab]a = 0, b = 1, n = 2000, so this is true since 1 < 2000
[tab]print b, [tab]1 is printed
[tab]a, b = b, a+b [tab]a = 1, b = 0+1 (zero is the old value of a)

Second time through:
while b < n: [tab]a = 1, b = 1, n = 2000, so this is true since 1 < 2000
[tab]print b, [tab]1 is printed
[tab]a, b = b, a+b [tab]a = 1, b = 1+1 (1 is the old value of a)

Third time through:
while b < n: [tab]a = 1, b = 2, n = 2000, so this is true since 2 < 2000
[tab]print b, [tab]2 is printed
[tab]a, b = b, a+b [tab]a = 2, b = 2+1

Fourth time through:
while b < n: [tab]a = 2, b = 3, n = 2000, so this is true since 3 < 2000
[tab]print b, [tab]3 is printed
[tab]a, b = b, a+b [tab]a = 3, b = 3+2

And so on. A C++ program might look like:

void feb(int n)
{
[tab]int a = 0;
[tab]int b = 1;

[tab]while (b < n)
[tab]{
[tab][tab]cout << b << &quot; &quot; << flush;
[tab][tab]int holda = a;
[tab][tab]a = b;
[tab][tab]b += holda;
[tab]}
}


Or something like it. Hope this helps.





[sig]<p>James P. Cottingham<br><a href=mailto: > </a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.[/sig]
 
Ok, I see the mistake I made.

I didn't realize that you put all the variables you wanted to sign, and then each of there assignments. You certainly don't do a,b = 1,2 in C++ ;)

Thanks for clearing that up :) [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top