## Reading notes: Chapter 3: Functions

A function is always defined before it is used. Python includes many pre-defined functions. When you import a Python library, as below done for Tkinter, functions are defined for use in the following program code.

from Tkinter import *

You have been using functions already. Here is one you used in p2.

def quitter():  # our quit function to call by pressing B_quit
W.destroy()
sys.exit()


This is a user-defined function because it is defined in your program. A function definition always begins with def, followed by the function name, then parentheses with any passed argument variable names inside, and it always ends with a colon. That is the first line defining the function.

Following statements within the function definition are indented after the colon, and indented uniformly. Use 3 or 4 spaces, never tabs. The IDLE editor will automatically indent lines after the colon for you.

When the function lines are done, backspace back to the original indentation before the indented function statements. Python requires exact indentation! Sequential statements indented together form a block.

Above, the function quitter() doesn't have any arguments. The statements W.destroy() and sys.exit() are function calls using a special syntax, and they are excuted one after another when quitter() is called--and that happens when the Quit button is pressed in your p2.py program. After the last stement is executed within the function, program flow returns to just after where the function was called. The quitter() function above, however, is different in that sys.exit() terminates the program right when called.

Read chapter 3 on functions, and work through the interactive examples with leading >>>.

## A Look Ahead

Chapter 4 is on conditionals (if, if..else) and recursion. This allows us to add logic and control to programs.

Sometimes programming projects just arise during the course of work, study, and just about anything else people do. Here is one, a FDLTCC work-related item.

Someone asked about help in surveying FDLTCC graduates. We send out forms, then the forms have to get mailed, we must receive them, then someone must enter/tabulate the responses. It isn't like we pester graduates just because we are nosey people. We like hearing what students do, and this helps us refine things to serve students now. Also, various external folks (accreditors and such) continually ask us about what FDLTCC graduates do, afterwards, after students are off in the world doing other interesting things.

The usual thing is mailing out forms, but that means a stamp and handling. What would be nice is an online survey, something like Survey Monkey, but something simpler and lighter we could offer directly.

Python code does not go online within web pages (the front-end), but Python is excellent for the server-side of handling such surveys, the back-end. In fact, one can write a stand-alone web server completely in Python using just a few lines of code. One can also write a CGI script which is a program called by a web server to issue the survey and collect results. This takes some extra work to duplicate on your own machine, a future project, but we can certainly do it.

That is one application which arose this past week, and will be done in Python as a CGI program. Keep open to such thoughts, things that might be done as a project in Python. They arise all the time.

Content is neither approved nor reviewed by FDLTCC.