Читать книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer - Страница 60

Function Syntax

Оглавление

Functions play a very important role in Python programming, and as we will see in the coming chapters, the use of packages depends significantly on the use of the functions that are within those packages. To thoroughly understand how functions work, we need to carefully examine function syntax. We show the syntax for defining a function in Python in Figure 2.16.

Description

Figure 2.16 Python Function Syntax

Syntax specifications involve several prominent features, including Python keywords in bold font, user-specified information in italicized font, and optional components specified within square brackets. The first line of every function definition in Python begins with the keyword def, followed by the function name and a set of parentheses in which you can specify an optional set of parameters, and then ends with a colon. The indented code statements that follow are part of the function. Optionally, a function can return one or more values. If you return more than one value, you need to separate each value with a comma. The specification of the function ends when a nonindented line of code occurs or if you reach the end of the file. We show a flowchart of the logic in using a function in Figure 2.17 to help visualize how a function works.

Description

Figure 2.17 Flowchart of Function Usage

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.

Introduction to Python Programming for Business and Social Science Applications

Подняться наверх