Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Onto the next part of Python: functions.
- Functions are essentially blocks of code that you can call at any time.
- So far we have used the built-in functions print x, str(x), len(x), type(x), range(x,y),
- String.insert(x,y) String.append(x), and String.remove(x)
- Now we will write our own.
- """
- #You make a function by starting with the keyword 'def'
- #Then you name the function, followed by parameters
- def giveMeFive():
- #the return keyword tells the computer what to give back at the end of the function
- return 5
- #Here's how you call the method. Really simple.
- #We used str() here, because giveMeFive() returns an integer
- print "Give me how many? "+str(giveMeFive())
- """
- This is also an important distinction - there is a function called giveMeFive, and a VALUE associated giveMeFive()
- Again, back to math, think of giveMeFive (the function) as f(x). giveMeFive() (the value) is it's y.
- giveMeFive() = 5
- f(x) = y
- """
- #the first type will be function, the second int.
- print "Type of giveMeFive: "+str(type(giveMeFive))+", Type of giveMeFive(): "+str(type(giveMeFive()))+"\n"
- """
- This is starting to err into the realm of functional programming (we will be using imperative programming,
- as it doesn't make your head want to explode - Wikipedia these if you want to know more), but just to
- show case a language feature: you can actually pass functions as values. Pretty neat.
- Java is very bad at this (although there are some very hacky ways you can get it to work). Scala can do this.
- This feature is in the languages Lisp, Scheme, and Haskell (and plenty of others).
- If you think this is cool, go look them up.
- """
- def functional():
- return giveMeFive
- #functionalValue will actually be the function giveMeFive
- #This means variables can be functions (procedures) as well as regular data.
- functionalValue = functional()
- print "Functional return type: "+str(type(functionalValue))+", value of functional()'s value: "+str(functionalValue())+"\n"
- #ANYWAY back on task!
- #Let's make a regular function...
- #This will print out each letter of the alphabet up to the maximum value.
- #The function is UNSAFE because if you supply a value greater than len(alpha)
- #then you create an error and the program crashes!
- def abcsUnsafe( amount ):
- #we make sure we have the alphabet in order
- alpha = "abcdefghijklmnopqrstuvwxyz"
- #Now we will return a result, but we have to create our result...
- result = ""
- #so just loop through that many letters
- for i in range(0, amount):
- #build the string by continually adding letters to what already exists
- result = result+alpha[i]+" "
- #hand it back.
- return result
- print "I know the first 3 letters of the alphabet! "+abcsUnsafe(3)+"\n"
- #Now, let's make a safe abcs using our previously unsafe function!
- def abcsSafe( amount ):
- result = ""
- while ( amount >= 26 ):
- result = result + abcsUnsafe(26)+"\n"
- amount = amount - 26
- #okay, so that will loop until you have less than 26, continually adding the alphabet!
- #This means you have any where from 0 to 26 left for the amount
- result = result + abcsUnsafe(amount)
- return result
- randomAmountOfABCS = 26*2+18
- print "I can say my ABCs a few times now! I got smarter pretty fast!\n"+abcsSafe(randomAmountOfABCS)
- print "All done.\n"
- #You can also load the function result into a variable, like we did for the functionalValue stuff
- someString = abcsSafe(5)
- print someString
Advertisement
Add Comment
Please, Sign In to add comment