This article assumes you're familiar with function definitions in at least one other computer language and with the concepts that correspond to function definitions, arguments, parameters, and so forth. As it does with control structures, Python uses indentation to delimit the body of the function definition. The second line is an optional documentation string or docstring. You can obtain its value by printing fact.__doc__. The intention of docstrings is to describe the external behavior of a function and the parameters it takes, whereas comments should document internal information about how the code works. Docstrings are strings that immediately follow the first line of a function definition and are usually triple quoted to allow for multiline descriptions. Browsing tools are available that extract the first line of document strings. It's a standard practice for multiline documentation strings to give a synopsis of the function in the first line, follow this with a blank second line, and end with the rest of the information. This line shows the value after the return is sent back to the code calling the function. In some languages, a function that doesn't return a value is called a procedure. Although you can (and will) write functions that don't have a return statement, they aren't really procedures.
All Python procedures are functions; if no explicit return is executed in the procedure body, then the special Python value None is returned, and if return arg is executed, then the value arg is immediately returned. Nothing else in the function body is executed once a return has been executed. Because Python doesn't have true procedures, we'll refer to both types as functions. Although all Python functions return values, it's up to you whether a function's return value is used. The return value isn't associated with a variable. The fact function's value is printed in the interpreter only . The return value is associated with the variable. Most functions need parameters, and each language has its own specifications for how function parameters are defined. Python is flexible and provides three options for defining function parameters. The simplest way to pass parameters to a function in Python is by position. In the first line of the function, you specify definition variable names for each parameter; when the function is called, the parameters used in the calling code are matched to the function's parameter variables based on their order. This method requires that the number of parameters used by the calling code exactly match the number of parameters in the function definition, or a TypeError exception will be raised. Any number of parameters can be given default values. Parameters with default values must be defined as the last parameters in the parameter list. This is because Python, like most languages, pairs arguments with parameters on a positional basis.
There must be enough arguments to a function that the last parameter in that function's parameter list that doesn't have a default value gets an argument. You can also pass arguments into a function using the name of the corresponding function parameter, rather than its position. Because the arguments to power in the final invocation of it are named, their order is irrelevant; the arguments are associated with the parameters of the same name in the definition of power. This type of argument passing is called keyword passing. Keyword passing, in combination with the default argument capability of Python functions, can be highly useful when you're defining functions with large numbers of possible arguments, most of which have common defaults. For example, consider a function that's intended to produce a list with information about files in the current directory and that uses Boolean arguments to indicate whether that list should include information such as file size, last modified date, and so forth, for each file. This type of argument handling is particularly suited for functions with very complex behavior, and one place such functions occur is in graphical user interfaces. If you ever use the Tkinter package to build GUIs in Python, you'll find that the use of optional, keyword-named arguments like this is invaluable.
Our website is not responsible for the information contained by this article. Articleinput.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.
Note: This article was sent to us by: Larry J. Harling at 01052010
1. How to make Python script execution in UNIX and Mac OS X and in Windows
All articles are property of their respective authors. Please read our Privacy Policy!
© 2009 ArticleInput.com.