


#!/usr/bin/env python
def add2(n):
return n + 2
def mul2(n):
return n * 2
def funk(n, op):
if hasattr(op, '__call__'): #see if object says you can call it
return op(n) #treat it like a function
else:
return "Second argument must be callable."
print(funk(3, add2)) #you pass a function by its name without '()'
print(funk(3, mul2))
print(funk(3, 3))


Arameus wrote:If I have a function such as:
def some_function(function_parameter)
print "stuff"
print "more stuff"
what would function parameter do? I have no understanding of this concept whatsoever. Here is a REAL example of code that uses function parameters, so you understand what I mean: http://inventwithpython.com/hangman.py
halp me
def hello(name)
print "Hello "
print namedef hello()
print "Hello whoever."
Bren2010 wrote:Arameus wrote:If I have a function such as:
def some_function(function_parameter)
print "stuff"
print "more stuff"
what would function parameter do? I have no understanding of this concept whatsoever. Here is a REAL example of code that uses function parameters, so you understand what I mean: http://inventwithpython.com/hangman.py
halp me
Function parameters are variables passed into a function for it to work.
You can have something like:
- Code: Select all
def hello(name)
print "Hello "
print name
Or you can leave the parameters section blank if you don't need any parameters, like:
- Code: Select all
def hello()
print "Hello whoever."

Arameus wrote:Hmm, I somewhat understand. Could you give me an example of a short program that absolutely requires the use of a function parameter? Sorry for being so needy, but this concept is simply going right over my head. Im a beginner, so make sure the example is super easy haha.
#!/usr/bin/env python2.6
def somefunct():
return "Some Text!"
def runfunct(functtorun):
print functtorun()
runfunct(somefunct)

Return to Interpreted Languages
Users browsing this forum: No registered users and 0 guests