Movatterモバイル変換


[0]ホーム

URL:


What does a function do?

Steve Holdensholden at cox.rr.com
Tue Apr 24 01:09:48 EDT 2001


"alki" <webmaster at pawntastic.com> wrote ...> So a function is just a group of codes? Then how do you use a function?Will> you please give me an example? Thanks>Suppose you wanted to say hello to several people. You could define afunction to say hello using a particular person's name. Such a functioncould be defined as follows...def sayhello(who):    print "Hello,", whoIf we then made the following calls:sayhello("alki")sayhello("steve")sayhello("everybody")then the program would print the following output, one line per functioncall:Hello, alkiHello, steveHello. everybodySo, as D-Man said, it's first and foremost an abbreviation for code whichyou want to use in several places (or even several programs). Of course, theexample above is a simple one, with one argument (who, the value that getspassed through fro the call), and not much code in it. In fact it would bequicker to just write the print statements!However, functions can get very complex, taking multiple argumnents, makingdecsisions and performing different processing based on the argument values,and so on.Does this help? Try writing a function of your own in the interactiveinterpreter now! You'll need to enter a blank line after the end of thedefinition, that's just the way the interpreter works interactively.regards Steve


More information about the Python-listmailing list

[8]ページ先頭

©2009-2025 Movatter.jp