Functions: Organizing and Reusing Code
A function is useful only when Python can understand how it is written and how it is used.
A Function Starts with Precise Language
A function is useful only when Python can understand how it is written and how it is used. This makes functions a good place to learn two foundations of programming: vocabulary and grammar. Vocabulary means the words Python recognizes, while grammar means the rules for arranging those words into valid statements. A spelling mistake or a missing grammatical part can prevent Python from understanding the instruction.
For functions, vocabulary includes the keyword def and the name chosen for the function. Grammar determines how those parts must be arranged. A function definition starts with def, followed by the function name, followed by parentheses and a colon. A function call must include parentheses. These rules are not optional formatting preferences: they allow Python to parse the statement.
Vocabulary and Grammar
Python vocabulary consists of words and symbols that Python recognizes, including keywords, identifiers, operators, and literals. Python grammar consists of the rules that govern how those parts are arranged to form valid statements.
The distinction is similar to learning a natural language. You need to know which words exist and how to spell them, but you also need to know how to arrange them into a meaningful sentence. In Python, the interpreter cannot infer your intention from a misspelled word or a malformed statement. It follows the language rules exactly as written.
A Well-Formed Function Statement
Checking a Function Definition and Call
Determine whether each statement follows the function rules described in the source material.
- 1Definition keyword
A function definition must start with def. The spelling must be exact; a different spelling is not the Python keyword.
- 2Function name
The keyword is followed by the function name. The chosen name must then be used consistently whenever the function is referred to.
- 3Parentheses
The function definition includes parentheses after the function name. A function call also includes parentheses.
- 4Colon
The function definition line ends with a colon. Omitting a required colon violates Python grammar and causes a syntax error.
- 5Call
A call uses the function name with parentheses. Leaving out the parentheses does not follow the stated function-call rule.
A function-related statement is well formed only when its vocabulary is spelled correctly and its required parts appear in the grammatical order.
The example uses the required function-definition pattern: def, a function name, parentheses, and a colon. The later function call includes the function name and parentheses. The example is intentionally focused on grammatical shape rather than on what the function does.
Valid and Invalid Syntax
| Part of Python | Correct idea | What goes wrong when it is changed |
|---|---|---|
| Keyword spelling | Use the exact keyword, such as if, while, or def | A misspelling such as iff or wile is not recognized as the keyword |
| Capitalization | Match every character exactly | Print and print are different |
| Function definition | Use def, a function name, parentheses, and a colon | Missing or misplaced parts create invalid syntax |
| Function call | Include parentheses | Leaving out the parentheses violates the function-call rule |
| Variable or function name | Use the same spelling every time | student_name, studentname, and student_Name are treated as different names |
Python vocabulary must be spelled precisely, and Python grammar determines how vocabulary is arranged.
This statement is invalid because the function-definition line is missing its colon. The source material identifies a missing colon as a grammatical violation that causes a syntax error. The computer cannot supply the missing character based on what the programmer probably intended.
Changing capitalization changes the name. Because Python is case-sensitive, Greet and greet are not the same spelling. A function name must be spelled consistently every time it is used.
Mistakes Beginners Make
Common mistake Changing the capitalization of a Python word
What it looks likeWriting Print when the Python word is print.
Why it failsPython is case-sensitive, so the two spellings are different.
Do this insteadMatch the capitalization of every character exactly.
Common mistake Misspelling a keyword
What it looks likeWriting iff instead of if or wile instead of while.
Why it failsPython has a fixed vocabulary of keywords. A misspelled word is not recognized as the intended keyword.
Do this insteadUse the exact spelling of the Python keyword.
Common mistake Leaving out the colon in a definition
What it looks likeWriting a function definition without the colon at the end.
Why it failsA function definition must end with a colon. Omitting it violates Python grammar and causes a syntax error.
Do this insteadCheck that the definition line contains def, the function name, parentheses, and a colon.
Common mistake Changing an identifier between uses
What it looks likeDefining student_name and later writing studentname or student_Name.
Why it failsPython treats differently spelled names as different variables or functions.
Do this insteadCopy the identifier's spelling consistently each time you use it.
Common mistake Calling a function without parentheses
What it looks likeWriting only the function name when making a function call.
Why it failsFunction calls must include parentheses.
Do this insteadPlace parentheses after the function name in the call.
When Python reports a syntax error, inspect the statement's vocabulary and grammar separately. First check spelling and capitalization. Then check whether required parts, such as parentheses or a colon, are present and arranged in the required order.
Practice the Language Rules
For each item, decide whether the problem is vocabulary, grammar, or neither: Print, iff, a function definition without its colon, a function call without parentheses, and a name whose capitalization changes between uses.
Hints
- Vocabulary problems involve spelling or capitalization.
- Grammar problems involve the arrangement or required parts of a statement.
- Use the function rules: def, function name, parentheses, and colon for a definition; parentheses for a call.
What do you think happens?
A function name is written as calculate_total in its definition. What happens if a later call uses calculateTotal instead?
Reveal answer
The two names are different because Python requires exact spelling and is case-sensitive.
Python treats names with different characters or capitalization as different identifiers. Names must be spelled consistently every time they are used.
What to Remember
- 1Python has vocabulary: keywords, identifiers, operators, and literals that must be spelled precisely.
- 2Python has grammar: rules for arranging vocabulary into valid statements.
- 3Python is case-sensitive, so capitalization is part of spelling.
- 4A function definition uses def, a function name, parentheses, and a colon.
- 5A function call includes parentheses, and violating required syntax causes a syntax error.
At a glance
- Python programming begins with mastering vocabulary and grammar.
- Every character in a Python keyword, variable name, or function name matters, including capitalization.
- Function definitions follow the pattern def, function name, parentheses, and colon.
- Function calls must include parentheses.
- Missing, misspelled, or misplaced parts produce invalid syntax that Python will not execute.