Need help with function parameters in Python

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Need help with function parameters in Python

Post by Arameus on Mon Feb 01, 2010 7:02 pm
([msg=34257]see Need help with function parameters in Python[/msg])

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
Arameus
New User
New User
 
Posts: 36
Joined: Mon Feb 01, 2010 6:53 pm
Location: Ballston Spa, NY
Blog: View Blog (0)


Re: Need help with function parameters in Python

Post by tgoe on Mon Feb 01, 2010 7:43 pm
([msg=34261]see Re: Need help with function parameters in Python[/msg])

I don't understand what it is that you're having trouble with :?
User avatar
tgoe
Contributor
Contributor
 
Posts: 527
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Need help with function parameters in Python

Post by Defience on Mon Feb 01, 2010 7:50 pm
([msg=34264]see Re: Need help with function parameters in Python[/msg])

Check out this article written by our very own, "thedotmaster" which explains modules, how to write them, how to import them, etc: http://www.hackthissite.org/articles/read/1056/
or:
http://www.penzilla.net/tutorials/python/functions/
User avatar
Defience
Addict
Addict
 
Posts: 1279
Joined: Thu Jun 12, 2008 3:16 pm
Blog: View Blog (0)


Re: Need help with function parameters in Python

Post by tgoe on Tue Feb 02, 2010 2:33 pm
([msg=34301]see Re: Need help with function parameters in Python[/msg])

If you mean passing functions around here is a small example:

Code: Select all
#!/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))
User avatar
tgoe
Contributor
Contributor
 
Posts: 527
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Need help with function parameters in Python

Post by insomaniacal on Tue Feb 02, 2010 3:40 pm
([msg=34304]see Re: Need help with function parameters in Python[/msg])

It's kind of the equivalent of storing the functions output as a global variable, then calling that global variable in a different function.
It's not who votes that counts, it's who counts the votes
insomaniacal.blog.com
User avatar
insomaniacal
Addict
Addict
 
Posts: 1212
Joined: Sun May 24, 2009 10:21 am
Blog: View Blog (0)


Re: Need help with function parameters in Python

Post by Bren2010 on Tue Feb 02, 2010 3:59 pm
([msg=34305]see Re: Need help with function parameters in Python[/msg])

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."
User avatar
Bren2010
Poster
Poster
 
Posts: 340
Joined: Fri Sep 19, 2008 3:23 pm
Blog: View Blog (0)


Re: Need help with function parameters in Python

Post by Arameus on Wed Feb 03, 2010 3:59 pm
([msg=34397]see Re: Need help with function parameters in Python[/msg])

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."



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.
Arameus
New User
New User
 
Posts: 36
Joined: Mon Feb 01, 2010 6:53 pm
Location: Ballston Spa, NY
Blog: View Blog (0)


Re: Need help with function parameters in Python

Post by faazshift on Wed Feb 03, 2010 4:58 pm
([msg=34402]see Re: Need help with function parameters in Python[/msg])

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.

Do you mean a function that requires a parameter to be given? or do you mean a function that requires a parameter that references a function?

Function required as parameter:
Code: Select all
#!/usr/bin/env python2.6

def somefunct():
    return "Some Text!"

def runfunct(functtorun):
    print functtorun()

runfunct(somefunct)

... which will pass somefunct as the function to run and print the return of. Result will be "Some Text!" being printed to the screen.

Is that what you are looking for?
faazshift
Contributor
Contributor
 
Posts: 522
Joined: Wed Jun 03, 2009 3:55 pm
Location: Riverton, Utah
Blog: View Blog (0)


Re: Need help with function parameters in Python

Post by Arameus on Mon Mar 29, 2010 3:21 pm
([msg=37444]see Re: Need help with function parameters in Python[/msg])

thanks guys, figured it out :D
Arameus
New User
New User
 
Posts: 36
Joined: Mon Feb 01, 2010 6:53 pm
Location: Ballston Spa, NY
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests