Writing Your First Function
The def keyword marks the start of a function definition and is required for every function.
The First Marker
A function definition needs a clear starting marker. In Python, that marker is the reserved keyword def. When Python sees def, it expects the definition to continue with a function name, parentheses for parameters, a colon, and the function body. Learning to recognize this structure is the first step in writing a function.
Reading a Definition
A function definition is organized around def. After def comes the function name, followed by parentheses, a colon, and the function body. The name is an identifier, so it must follow the same naming rules as a variable. The parentheses provide the place for parameters, and the colon marks the point after which the function body begins.
In this generated example, def marks the beginning, greet_user is the function name, the empty parentheses show the parameter position, and the colon introduces the body. The example uses snake_case, the naming convention Python strongly favors for function names.
Choosing a Valid Name
Function names follow exactly the same rules as variable names. A valid name starts with a letter or an underscore. After that first character, it may contain letters, numbers, and underscores. It cannot be a Python keyword. These are naming constraints, not merely style preferences.
| Rule | Generated example | Reason |
|---|---|---|
| Start with a letter or underscore | calculate_total | The first character is a letter |
| Numbers may appear after the first character | user2 | The number is not at the beginning |
| Use only letters, numbers, and underscores | user_name | The name uses letters and an underscore |
| Do not use a Python keyword | if | if is reserved by Python |
Function names use the same identifier rules as variable names.
Python allows both snake_case and camelCase under its naming rules, but Python convention strongly favors snake_case for function names. Names such as calculate_total and send_message follow that convention and are easier to read consistently with other Python code.
Avoiding Name Collisions
A function name and a variable name should not be reused in the same scope. If a function is given a name and that same name is later assigned to a variable, the variable assignment overwrites the function. The function then becomes inaccessible through that name. This can produce confusing behavior because the code may still look as though the function exists.
Mistakes to Catch Early
Treating function names as if they had different rules from variable names
Function names and variable names follow the same naming constraints.
Fix:
Check that the name starts with a letter or underscore, uses only letters, numbers, and underscores, and is not a Python keyword.Starting a function name with a number
A valid function name cannot begin with a number.
Fix:
Place the number later in the name or choose a name such as users2.Using a space, hyphen, or other invalid character in a function name
Function names may contain only letters, numbers, and underscores.
Fix:
Use underscores, as in send_message.Using a Python keyword as a function name
Keywords are reserved for Python syntax and cannot be used as function or variable names.
Fix:
Choose a different available identifier.Reusing a function name for a variable in the same scope
The variable assignment overwrites the function, making it inaccessible through that name.
Fix:
Give the variable and function different names.Using camelCase when the project follows Python convention
The name may satisfy Python's naming rules, but Python convention strongly favors snake_case for function names.
Fix:
Prefer calculate_total for consistent, readable Python code.
Name Check Practice
For each proposed function name, decide whether it follows Python's naming rules. If it is invalid, identify the rule it breaks. If it is valid but not written in the preferred convention, rewrite it in snake_case: calculate_total, 3items, send-message, class, user_count, calculateTotal.
Hints
- Check the first character before checking the rest of the name.
- Look for characters other than letters, numbers, and underscores.
- Remember that Python keywords cannot be used as names.
- A valid camelCase name can still be rewritten to the preferred snake_case convention.
Checking a Proposed Function Name
Evaluate the generated name calculateTotal.
Check the first character: The name begins with a letter, so it passes the first-character rule.
Check the remaining characters: The name contains letters only, so it passes the allowed-character rule.
Check for a keyword: The proposed name is not the Python keyword def or another reserved keyword such as if, for, or class.
Apply the convention: The name is valid under Python's naming rules, but Python convention favors snake_case for function names.
calculateTotal is a valid function name, but calculate_total is the preferred conventional form.
Key Takeaways
- Every Python function definition begins with the def keyword.
- Function names follow the same rules as variable names: start with a letter or underscore, use only letters, numbers, and underscores, and avoid Python keywords.
- Python convention strongly favors snake_case for function names, even though camelCase can satisfy the basic naming rules.
- Do not use the same name for a function and a variable in the same scope because the variable assignment overwrites the function.
- Good function naming prevents syntax problems, confusing code, and accidental loss of access to a function.
Key Takeaways
- def marks the start of every function definition.
- Function names use exactly the same naming rules as variable names.
- Names cannot begin with numbers, contain invalid characters, or be Python keywords.
- snake_case is the preferred Python convention for function names.
- Using the same name for a function and a variable in the same scope causes the variable assignment to overwrite the function.