The following function performs
some basic math on values provided at the time of the function call.
This function demonstrates how to perform the calculations, as well as
how to format the program output to the display.
>>> def addnum(a, b):
# Remind the user of the values he/she has input.
print "Variable 'a' equals", a, "and variable 'b' equals " + str(b) + "."
# While a is less than or equal to b, carry out the following steps.
while a <= b:
# Print the value of a subtracted from b.
print b, "minus variable 'a' equals " + str(b - a) + "."
# Display a statement that a is added to itself.
print "Variable 'a' is added to its own value."
# Add the value of a to itself and assign the new value back to a.
a = a + a
# Print the new value of a.
print "Variable 'a' now equals " + str(a) + "."
The function is called by entering its name, followed by its parameters (the values *a* and *b*) in parentheses. Here follows a sample session.
>>> addnum(1, 15)
Variable 'a' equals 1 and variable 'b' equals 15.
15 minus variable 'a' equals 14.
Variable 'a' is added to its own value.
Variable 'a' now equals 2.
15 minus variable 'a' equals 13.
Variable 'a' is added to its own value.
Variable 'a' now equals 4.
15 minus variable 'a' equals 11.
Variable 'a' is added to its own value.
Variable 'a' now equals 8.
15 minus variable 'a' equals 7.
Variable 'a' is added to its own value.
Variable 'a' now equals 16.
>>>