Читать книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer - Страница 61
Using Functions
ОглавлениеThe simplest form of a function is one that does not have any parameters and does not return any values. Figure 2.18 shows an example of such a function that prints out a message. Functions in Python only execute when you invoke them. To invoke a function, you use the function name, along with the specification of function parameters (if there are any). Because the function defined in lines 1 and 2 of the Python code in Figure 2.18 does not have any parameters, empty parentheses follow the function name.
Figure 2.18 Simple Function with No Parameters
Figure 2.19 illustrates the output that results when executing the Python code in Figure 2.18.
Figure 2.19 Output of Program in Figure 2.18
Functions become more dynamic when you pass parameters to them. Actions performed by the statements in a function can change when it receives different information. Figure 2.20 shows an example of such a function that receives a name and prints out a different message depending on what name it receives.
Figure 2.20 Simple Function with Parameters
Figure 2.21 shows the output that results from executing the Python code in Figure 2.20. As you can see, the code prints two different messages, because we pass a different name to the function each time. This simple example illustrates the ability of functions to do different things depending on what information they receive.
Figure 2.21 Output of Program in Figure 2.20
When functions have parameters, the possibility of errors that can occur increases. Two common errors are specifying the incorrect number of parameters and using incorrect data types of parameters. The Python code in Figure 2.22 illustrates such an error, where line 4 invokes the function but attempts to pass two parameters.
Figure 2.22 Error using Function with Incorrect Parameters
Figure 2.23 illustrates the error that occurs when executing the code in Figure 2.22. The error message is very helpful. The message identifies the line number and shows the code that caused the error. The last line of the error message specifies that a TypeError occurred and that the specific function takes one positional argument (parameter), and the function received two.
Description
Figure 2.23 Output of Program in Figure 2.22
Functions can become even more useful when they return a value or values. To illustrate how returning a value works, the next example returns a string from the function and then prints the result that the function returns.
Figure 2.24 Function Returning a Value
The Python code in Figure 2.24 invokes the function two different times in two different ways. The code in line 4 invokes the function and assigns the value that the function returns to a variable named message. Then line 5 uses the print function to print out the value of the variable message. Line 6 uses the user-defined function within the print function. The output of this code execution is in Figure 2.25, illustrating that the different approaches yield similar results.
Figure 2.25 Output of Program with Function Returning a Value
Figure 2.26 illustrates an example of a function that receives two parameters and returns a value that is based on the data it receives.
Description
Figure 2.26 The find_average Function
The Python code following the function definition in Figure 2.26 creates two integer variables in lines 7 and 8 and then invokes the find_average function within the code in line 11. Note that the two arguments that the function receives have different variable names than the names of the arguments used in the definition of the function. The arguments passed can take other forms as well, such as actual values or the result of some operation or calculation. The function only has one line of code, which is to return the value that results from dividing the sum of the two received arguments by the value 3. The output from executing the code in Figure 2.26 is in Figure 2.27.
Figure 2.27 Output from Execution of find_average Function