omegazero

functions.py

Jun 21st, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. """
  2. Onto the next part of Python: functions.
  3. Functions are essentially blocks of code that you can call at any time.
  4. So far we have used the built-in functions print x, str(x), len(x), type(x), range(x,y),
  5. String.insert(x,y) String.append(x), and String.remove(x)
  6.  
  7. Now we will write our own.
  8. """
  9. #You make a function by starting with the keyword 'def'
  10. #Then you name the function, followed by parameters
  11. def giveMeFive():
  12.     #the return keyword tells the computer what to give back at the end of the function
  13.     return 5
  14.  
  15. #Here's how you call the method. Really simple.
  16. #We used str() here, because giveMeFive() returns an integer
  17. print "Give me how many? "+str(giveMeFive())
  18.  
  19. """
  20. This is also an important distinction - there is a function called giveMeFive, and a VALUE associated giveMeFive()
  21. Again, back to math, think of giveMeFive (the function) as f(x). giveMeFive() (the value) is it's y.
  22. giveMeFive() = 5
  23.        f(x) = y
  24. """
  25.  
  26. #the first type will be function, the second int.
  27. print "Type of giveMeFive: "+str(type(giveMeFive))+", Type of giveMeFive(): "+str(type(giveMeFive()))+"\n"
  28.  
  29. """
  30. This is starting to err into the realm of functional programming (we will be using imperative programming,
  31. as it doesn't make your head want to explode - Wikipedia these if you want to know more), but just to
  32. show case a language feature: you can  actually pass functions as values. Pretty neat.
  33. Java is very bad at this (although there are some very hacky ways you can get it to work). Scala can do this.
  34. This feature is in the languages Lisp, Scheme, and Haskell (and plenty of others).
  35. If you think this is cool, go look them up.
  36. """
  37. def functional():
  38.     return giveMeFive
  39. #functionalValue will actually be the function giveMeFive
  40. #This means variables can be functions (procedures) as well as regular data.
  41. functionalValue = functional()
  42. print "Functional return type: "+str(type(functionalValue))+", value of functional()'s value: "+str(functionalValue())+"\n"
  43.  
  44. #ANYWAY back on task!
  45. #Let's make a regular function...
  46. #This will print out each letter of the alphabet up to the maximum value.
  47. #The function is UNSAFE because if you supply a value greater than len(alpha)
  48. #then you create an error and the program crashes!
  49.  
  50. def abcsUnsafe( amount ):
  51.     #we make sure we have the alphabet in order
  52.     alpha = "abcdefghijklmnopqrstuvwxyz"
  53.     #Now we will return a result, but we have to create our result...
  54.     result = ""
  55.     #so just loop through that many letters
  56.     for i in range(0, amount):
  57.         #build the string by continually adding letters to what already exists
  58.         result = result+alpha[i]+" "
  59.     #hand it back.
  60.     return result
  61.    
  62. print "I know the first 3 letters of the alphabet! "+abcsUnsafe(3)+"\n"
  63.  
  64. #Now, let's make a safe abcs using our previously unsafe function!
  65. def abcsSafe( amount ):
  66.     result = ""
  67.     while ( amount >= 26 ):
  68.         result = result + abcsUnsafe(26)+"\n"
  69.         amount = amount - 26
  70.     #okay, so that will loop until you have less than 26, continually adding the alphabet!
  71.     #This means you have any where from 0 to 26 left for the amount
  72.     result = result + abcsUnsafe(amount)
  73.     return result
  74.    
  75. randomAmountOfABCS = 26*2+18
  76. print "I can say my ABCs a few times now! I got smarter pretty fast!\n"+abcsSafe(randomAmountOfABCS)
  77. print "All done.\n"
  78. #You can also load the function result into a variable, like we did for the functionalValue stuff
  79. someString = abcsSafe(5)
  80. print someString
Advertisement
Add Comment
Please, Sign In to add comment