Skip to main content

Foundations of Python Programming: Functions First

Section 4.2 Function Definition

The syntax for creating a named function, a function definition, is:
def name( parameters ):
You can make up any names you want for the functions you create, except that you can’t use a name that is a Python keyword, and the names must follow the rules for legal identifiers that were previously explained for variables.
It is good practice to choose function names that are meaningful to the human readers of the program. This means the names will need to be unique and to make clear the function’s purpose. Since functions ’do’ something, some programmers like to use verbs in their function names.
def find_Average (grades:list ) -> float:

def increase (number:int, by=2) -> int:
The parameters specify what information, if any, you have to provide in order to use the new function. Another way to say this is that the parameters specify what the function needs to do its work. Notice that in the example function headers above, the programmer has included hints to indicate the parameter’s data type and/or its value. And, the data type of the function’s result has been annotated too. Be sure to check out the text’s later section \ on "Type Annotations" for more explanation on these ideas.
There can be any number of statements inside the function’s body, but they have to be indented from the def. Function definitions are one of several compound statements we will see, all of which have the same pattern:
  1. A header line which begins with a keyword and ends with a colon, :.
  2. A body consisting of one or more Python statements, each indented the same amount – 4 spaces is the Python standard – from the header line.
Later we will see conditionals,if, elif, and else statements, and interations, for and while statements, that have the same strucure where each has an indented block of code.
In a function definition, the keyword in the header is def, which is followed by the name of the function and some parameter names enclosed in parentheses. The parameter list may be empty, or it may contain any number of parameters separated from one another by commas. In either case, the parentheses are required.
We will come back to the parameters in a little while, but first let’s see what happens when a function is executed, using a function without any parameters to illustrate.
Here’s the definition of a simple function, hello.

Note 4.2.1. docstrings.

By convention, Python programmers use a triple-quoted string as the first line(s) of the function’s body, to have one- or two-sentences used to explain their functions, making clear the function’s parameters (data type requirements, limitations to their values, default values if set) and the return value (if there is one).
This unassigned string literal is called a docstring (documentation string) and because it gets special treatment in Python, you can use the interactive interpreter at runtime, and enter the expression <function_name>.__doc__, or help(<function_name>), to retrieve a function’s docstring (and thus its explanation). This is different from comments in your code, which are completely ignored when the program is run.
You have attempted of activities on this page.