Introduction to Python Programming for Business and Social Science Applications. Frederick Kaefer

Чтение книги онлайн.

Читать онлайн книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer страница 14

Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer

Скачать книгу

      The logic shown in Figure 2.17 shows that when you encounter a code statement that uses a function, you pass control to the function code. You can optionally pass parameters to the function, which the function uses in its processing. The function comprises code statements and optionally may return values back to the calling code. When the function ends, the control of code execution resumes in the calling code.

      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.

A screenshot displays three lines of code in a Python file as follows. Line 1: def hello(): Line 2: print (“Hello, John!”). Line 4: hello().

      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.

A screenshot displays the output of a program. The output is titled, RESTART: I:\Fig 2_18 simple function with no parameters.py. There is one line of output as follows. Line 1: Hello, John!

      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.

A screenshot displays three lines of code in a Python file as follows. Lines 1 and 2: def hello(name): print(“Hello, ” + name + “!”). Line 4: hello(“John”). Line 5: hello(“Jane”).

      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.

A screenshot displays the output of a program. The output is titled, RESTART: I:\Fig 2_20 simple function with parameter.py. There are two lines of output as follows. Line 1: Hello, John! Line 2: Hello, Jane!

      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.

A screenshot displays three lines of code in a Python file as follows. Lines 1 and 2: def hello(name): print(“Hello, ” + name + “!”). Line 4: hello(“John”, “Joe”). Line 5: hello(“Jane”).

      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.

      A screenshot displays the output of a program that displays an error when a function is used with incorrect parameters.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.

A screenshot displays four lines of code in a Python file as follows. Lines 1 and 2: def hello(name): return(“Hello, ” + name + “!”). Line 4: message = hello(“John”). Line 5: print(message). Line 6: print(hello(“Jane”)).

      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.

A screenshot displays the output of a program. The output is titled, RESTART: I:\Fig 2_24 function returning a value.py. There are two lines of output as follows. Line 1: Hello, John! Line 2: Hello, Jane!

      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.

      A screenshot displays the code lines for the find_average function in a Python file.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.

A screenshot displays the output of a program. The output is titled, RESTART: I:\Fig 2_26 Find_Average function.py. There is one line of output as follows. Line 1: The average of the two numbers is: 6.0.

      Figure 2.27 Output from Execution of find_average Function

      Function Location in Code

      When Python code is in a text file, you can define functions anywhere within the file and in any order when there are multiple functions. However, it is important that the function definition occurs prior to the use of the function, and so a convention commonly followed when writing Python code is to have all the functions defined at the beginning of the file and, if

Скачать книгу