Definitions and uses of functions in Python


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.

Legal Disclaimer

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

Related Articles

1. How to make Python script execution in UNIX and Mac OS X and in Windows
If you're on UNIX, you can easily make a script directly executable. Note that if Python 3.x isn't your default version of Python, you may need...

2. The difference between scripts on Windows scripts on UNIX
The way you call scripts on Windows differs from the way scripts are called on Linux/ UNIX, and that difference can affect what kind of scripts...

3. Python applications are distributed as source files
You can distribute your scripts as source files (as .py files). You can also ship them as byte code (as .pyc or .pyo files). A byte code file w...

4. How to use the Python file system
Working with files involves one of two things: basic I/O and working with the filesystem (for example, naming, creating, moving, or referring t...

5. Client complaints about inconsistent query results
You've probably seen it before: a client has called the help desk and reported that a couple of queries have seemingly inconsistent results. Yo...

6. How to ensure data quality
The most fundamental thing you can do to ensure the quality of your data is to choose a reasonable container for the value being stored. Want t...

7. How to use a foreign key constraint
A foreign key constraint is used to make sure that columns involved in a relationship contain only correct data. I start out the article with a...

8. Triggers in SQL Server programming
Triggers are stored batches of T-SQL, much like stored procedures. But instead of being called directly, they execute indirectly when you execu...

9. How to apply the tools to common situations
It's important to consider how to apply all of these tools to common situations. Bear in mind that for the most part, most of the situations we...

10. Communication between domain experts and data modelers
Some people will tell you that properly normalizing your tables is hard. It's not. The normalization rules are clear, and once you know all the...