Concepts / Functions: Organizing and Reusing Code

Functions: Organizing and Reusing Code

A function is useful only when Python can understand how it is written and how it is used.

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

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

Vocabulary and Grammar

Definition

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.

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

A Well-Formed Function Statement

Worked example

Checking a Function Definition and Call

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

  1. 1
    Definition keyword

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

  2. 2
    Function name

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

  3. 3
    Parentheses

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

  4. 4
    Colon

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

  5. 5
    Call

    A call uses the function name with parentheses. Leaving out the parentheses does not follow the stated function-call rule.

Result

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

Valid statementrequired parts presentSyntax errorrule violatedafter omission or misordering
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 vocabulary must be spelled precisely, and Python grammar determines how vocabulary is arranged.

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

  • Common mistake

    Changing the capitalization of a Python word

    What it looks like

    Writing Print when the Python word is print.

    Why it fails

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

    Do this instead

    Match the capitalization of every character exactly.

  • Common mistake

    Misspelling a keyword

    What it looks like

    Writing iff instead of if or wile instead of while.

    Why it fails

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

    Do this instead

    Use the exact spelling of the Python keyword.

  • Common mistake

    Leaving out the colon in a definition

    What it looks like

    Writing a function definition without the colon at the end.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Changing an identifier between uses

    What it looks like

    Defining student_name and later writing studentname or student_Name.

    Why it fails

    Python treats differently spelled names as different variables or functions.

    Do this instead

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

  • Common mistake

    Calling a function without parentheses

    What it looks like

    Writing only the function name when making a function call.

    Why it fails

    Function calls must include parentheses.

    Do this instead

    Place parentheses after the function name in the call.

Best practice

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. 1
    Python has vocabulary: keywords, identifiers, operators, and literals that must be spelled precisely.
  2. 2
    Python has grammar: rules for arranging vocabulary into valid statements.
  3. 3
    Python is case-sensitive, so capitalization is part of spelling.
  4. 4
    A function definition uses def, a function name, parentheses, and a colon.
  5. 5
    A 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.

Related concepts

More concepts starting with F