iNTERFACEWARE Products Manual > Learning Center > Learning Python > Functions and Global Variables > Introduction to Functions |
|
Looking for Iguana v.5 or v.6? Learn More or see the Help Center.
In Python, a function is a separate chunk of code that performs a specific task or set of tasks. To define a function, use the def statement. For example, here is a definition of a very simple function:
The def statement defines a function named print_HL7_field_delimiter that prints an HL7 field delimiter character on the screen. Note that statements inside functions, such as the print statement in this example, must always be indented. The print_HL7_field_delimiter() statement tells Python to execute the code contained in the print_HL7_field_delimiter function. This is sometimes referred to as a function call or as an invocation of the function. In Python, a function must be defined before it can be called. The following code generates an error:
The print_HL7_field_delimiter() statement generates an error because Python has not yet seen a definition of print_HL7_field_delimiter.
Note that functions can call other functions. This enables you to modularize your program: in other words, divide your task into smaller tasks, which can then be divided into smaller tasks, and so on. |