Concepts / Functions: Organizing and Reusing Code

Functions: Organizing and Reusing Code

Programming languages, like natural languages, have vocabulary (words that must be spelled correctly) and grammar (rules for arranging those words into valid statements).

  • Programming

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.

used throughFunction definitiondef, name, parentheses,colonFunction callfunction name, parentheses
How does defining a function establish a form that can later be used by a function call?

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.

followed byfollowed byfollowed bydeffunction-definition keywordfunction nameidentifier()required punctuation:ends the definition line
How do correctly spelled Python words combine according to grammatical rules to form a valid function definition?

A Well-Formed Function Statement

Checking a Function Definition and Call

Determine whether each statement follows the function rules described in the source material.

Definition keyword: A function definition must start with def. The spelling must be exact; a different spelling is not the Python keyword.

Function name: The keyword is followed by the function name. The chosen name must then be used consistently whenever the function is referred to.

Parentheses: The function definition includes parentheses after the function name. A function call also includes parentheses.

Colon: The function definition line ends with a colon. Omitting a required colon violates Python grammar and causes a syntax error.

Call: 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.

python

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

after omission or misorderingValid statementrequired parts presentSyntax errorrule violated
What changes between a correctly formed Python statement and one that produces a syntax error?
Part of PythonCorrect ideaWhat goes wrong when it is changed
Keyword spellingUse the exact keyword, such as if, while, or defA misspelling such as iff or wile is not recognized as the keyword
CapitalizationMatch every character exactlyPrint and print are different
Function definitionUse def, a function name, parentheses, and a colonMissing or misplaced parts create invalid syntax
Function callInclude parenthesesLeaving out the parentheses violates the function-call rule
Variable or function nameUse the same spelling every timestudent_name, studentname, and student_Name are treated as different names
python

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.

python

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

  • Changing the capitalization of a Python word

    Python is case-sensitive, so the two spellings are different.

    Fix: Match the capitalization of every character exactly.

  • Misspelling a keyword

    Python has a fixed vocabulary of keywords. A misspelled word is not recognized as the intended keyword.

    Fix: Use the exact spelling of the Python keyword.

  • Leaving out the colon in a definition

    A function definition must end with a colon. Omitting it violates Python grammar and causes a syntax error.

    Fix: Check that the definition line contains def, the function name, parentheses, and a colon.

  • Changing an identifier between uses

    Python treats differently spelled names as different variables or functions.

    Fix: Copy the identifier's spelling consistently each time you use it.

  • Calling a function without parentheses

    Function calls must include parentheses.

    Fix: Place 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

EASY

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

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

  1. Python has vocabulary: keywords, identifiers, operators, and literals that must be spelled precisely.
  2. Python has grammar: rules for arranging vocabulary into valid statements.
  3. Python is case-sensitive, so capitalization is part of spelling.
  4. A function definition uses def, a function name, parentheses, and a colon.
  5. A function call includes parentheses, and violating required syntax causes a syntax error.

Key Takeaways

  • 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.