Skip to content

Instantly share code, notes, and snippets.

@dhconnelly
Created March 29, 2012 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhconnelly/2236599 to your computer and use it in GitHub Desktop.
Save dhconnelly/2236599 to your computer and use it in GitHub Desktop.
brief tutorial on functions for GSMST number theory
# -----------------------------------------------------------------------------
# A brief tutorial on functions
# by Daniel Connelly (dhconnelly@gmail.com)
# -----------------------------------------------------------------------------
# Sometimes we want to re-use a piece of programming logic. Copy-and-paste is
# not necessarily a good approach, as it is prone to errors and duplicates
# work. A good maxim is "Don't Repeat Yourself." Let's see how we can use
# functions to eliminate repetition.
print
print 'Repeated work...'
# Let's say we're writing a program that involves a lot of exponents.
# First we just want to find a specific result:
base = 3
exp = 5
prod = 1
while exp > 0:
prod *= base # same as prod = prod * base
exp -= 1 # same as exp = exp - 1
print '3 ^ 5 =', prod
# Now we want to provide a basic exponent calculator to the user.
while raw_input('Compute an exponent? ') == 'yes':
base = float(raw_input('Base: '))
exp = float(raw_input('Pow: '))
prod = 1
while exp > 0:
prod *= base
exp -= 1
print prod
# Now we want to print the powers of 2 up to 2^30.
i = 1
while i <= 30:
prod = 1
j = 0
while j < i:
prod *= 2
j += 1
print '2 ^', i, '=', prod
i += 1
# Alright, that's enough. You should notice that the procedure for computing
# the exponent was the same each time. So that we don't accidentally mess it
# up and to eliminate the repeated work, we'll make this a function.
# First we have to define our function. We'll call it "power". Right now it
# will never compute the right answer:
def power(a, b):
return 0
# Before we talk about how this works, let's see what it would *look like* if
# we replaced the code to compute the exponents with a function.
print
print 'Incorrect function definition...'
print '3 ^ 5 =', power(3, 5)
# That was so easy. We write the "name" of the function we want to use (in
# this case, "power"), write parentheses, and insert the "arguments"--the
# numbers that the function should use to compute the exponent.
while raw_input('Compute an exponent? ') == 'yes':
base = float(raw_input('Base: '))
exp = float(raw_input('Pow: '))
print power(base, exp)
# Also easy. We replace all the work to compute the exponent with a single
# "call" to the "power" function.
i = 1
while i <= 30:
print '2 ^', i, '=', power(2, i)
i += 1
# That worked the same way.
# Now we need to make our function work properly. Let's examine the "function
# definition" we made before to see what it means:
def power(a, b):
return 0
# The "def" part means that we are defining a new function. "def" is *not* a
# function itself--it just means that we're about to write a new function.
#
# The next part, "power", is the name of the function we're defining.
#
# Next we have to write some parentheses: (). These will surround the
# "parameters" to the function. In this case, we want to accept two, which we
# will call "a" and "b". Those variables are ONLY visible INSIDE the function
# definition--they're what WE will call the two numbers that we're told to
# use.
#
# Finally, "return" says that whatever follows it should be the value that
# replaces any call to our function. So anywhere someone uses our "power"
# function, it will be replaced by 0 (right now). For example, we wrote
# "power(3, 5)" above. That will be replaced by 0.
# Now that we understand the parts of the function definition, let's make it
# work properly:
def power(a, b):
prod = 1
while b > 0:
prod *= a
b -= 1
return prod
# Notice that each time we computed exponents above, it had this basic form.
# We can use this to do all the same work we had before. Let's see:
print
print 'Correct function definition!'
print '3 ^ 5 =', power(3, 5)
while raw_input('Compute an exponent? ') == 'yes':
base = float(raw_input('Base: '))
exp = float(raw_input('Pow: '))
print power(base, exp)
i = 1
while i <= 30:
print '2 ^', i, '=', power(2, i)
i += 1
# Alright, now we understand functions. (Maybe.) Let's move on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment