Page 154 - Beginning Programming with Pyth - John Paul Mueller
P. 154

to
Reduce development time
Reduce programmer error
Increase application reliability
Allow entire groups to benefit from the work of one programmer Make code easier to understand
Improve application efficiency
In fact, functions do a whole list of things for applications in the form of reusability. As you work through the examples in this book, you see how reusability makes your life significantly easier. If not for reusability, you’d still be programming by plugging 0s and 1s into the computer by hand.
Defining a function
Creating a function doesn’t require much work. Python tends to make things fast and easy for you. The following steps show you the process of creating a function that you can later access:
1. Create a new notebook in Notebook.
The book uses the filename BPPD_07_Managing_Information.ipynb, which is where you find all the source code for this chapter. See the Introduction for information on using the downloadable source.
2. Type def Hello(): and press Enter.
This step tells Python to define a function named Hello. The parentheses are important because they define any requirements for using the function. (There aren't any requirements in this case.) The colon at the end tells Python that you’re done defining the way in which people will access the function. Notice that the insertion pointer is now indented, as shown in Figure 7-1. This indentation is a reminder that you must give the function a task to perform.
3. Type print(“This is my first Python function!”) and press Enter. You should notice something special, as shown in Figure 7-2. The
  




















































































   152   153   154   155   156