Concepts / Functions and How to Call Them

Functions and How to Call Them

A function is used through a particular piece of syntax.

  • Programming

A Precise Conversation

When you write Python, you are giving instructions to a language processor. Python does not guess what you intended. It checks whether your instructions follow Python's syntax rules. If they do not, Python rejects the code instead of trying to repair it for you. Even one missing or misplaced character can prevent the line from running.

What a Function Call Looks Like

A function is used through a particular piece of syntax. In the source example, Python uses the print function to display text on the screen. The call begins with the word print, followed by parentheses. The text goes inside the parentheses and is enclosed in quotation marks. Python recognizes this arrangement as a valid instruction and executes it.

printfunction name(opening parenthesis"Hello world!"quoted text)closing parenthesisfollowed bycontainsfollowed by
What characters and positions must be present for Python to recognize a print function call?
python
Output
Hello world!

The important point is not just the word print. Python also needs the surrounding structure: parentheses and matching quotation marks around the text. When all of those parts are present in the expected positions, Python accepts the instruction and produces the text on the screen.

One Character Can Stop Execution

Worked example

Finding a missing quote

A print call contains text that begins with a quotation mark but does not end with a matching quotation mark.

  1. 1
    Inspect the call

    The print function call starts correctly, but the text is not closed with the quotation mark Python expects.

  2. 2
    Read the error description

    Python reports an unterminated string literal. This means it found an opening quote but never found the closing quote needed to finish the string.

  3. 3
    Return to the reported line

    Use the line number and the pointer shown by Python to locate the area where the problem was detected.

  4. 4
    Add the missing character

    Place the matching closing quote at the end of the text, then try the instruction again.

Result

The corrected call follows the required pattern: print, parentheses, text enclosed by matching quotes, and a closing parenthesis.

print('say)opening quote has no matchprint('say')matching quotesSyntaxErrorunterminated string literalsayinstruction acceptedPython rejectsPython executes
How does Python point to the location of a syntax mistake, and how is that location connected to the invalid code?

This example demonstrates why syntax errors can feel disproportionate to the mistake. The missing character is small, but Python cannot safely execute a line when it cannot determine where the text ends. The entire line is rejected until the required closing quote is supplied.

Reading SyntaxError Messages

Definition

A SyntaxError means that your code violates Python's syntax rules. The message gives information that helps you locate and correct the problem.

  • The file and line number where Python encountered the problem.
  • The line of code associated with the error.
  • A pointer, shown with carets, indicating approximately where the problem was detected.
  • The error name, such as SyntaxError.
  • A description of what Python believes went wrong, such as unterminated string literal.

The pointer does not mean that the character directly under it is always the original cause. It shows where Python detected that the code no longer fit the rules. Use it together with the line and the description. For an unterminated string literal, the useful investigation is to check the quotation marks on that line and confirm that an opening quote has a matching closing quote.

Message informationQuestion to ask
Line numberWhich line should I inspect first?
Code lineWhat did Python receive?
PointerNear which location did Python detect the violation?
Error nameWhat category of problem occurred?
DescriptionWhat rule appears to have been violated?

Use all parts of a SyntaxError message together rather than focusing on the pointer alone.

The Correction Cycle

Syntax errors are part of learning Python, not evidence that the learning process has failed. The normal process is iterative: write an instruction, let Python accept or reject it, read the error if it is rejected, identify the problem, correct the code, and try again. Repeating this cycle teaches you which forms Python accepts.

Write codePython checks syntaxExecute instructionvalid syntaxSyntaxErrorinvalid syntaxRead error messageFix codesubmitacceptedrejectedinspectidentify problemtry again
What happens after Python rejects code, and how does the error message guide the next correction?
Best practice

When Python reports a syntax error, do not immediately rewrite the whole program. Start with the reported line. Read the error name and description, inspect the nearby punctuation, and then make a focused correction.

Mistakes to Catch Early

  • Common mistake

    Forgetting the parentheses after print.

    What it looks like

    The word print is written without the parentheses that surround the text.

    Why it fails

    The source describes the print call as the word print followed by parentheses with the text inside. Leaving out that structure does not match the required syntax.

    Do this instead

    Check that the function name is followed by opening and closing parentheses.

  • Common mistake

    Leaving a string without matching quotation marks.

    What it looks like

    A text value begins with a quote but has no closing quote.

    Why it fails

    Python cannot determine where the text ends and reports an unterminated string literal.

    Do this instead

    Check every opening quotation mark and add its matching closing quotation mark.

  • Common mistake

    Writing natural language instead of a Python command.

    What it looks like

    A sentence intended as a greeting is entered as though Python understands ordinary English.

    Why it fails

    Python understands Python syntax, not English sentences, so it rejects the input as invalid Python code.

    Do this instead

    Express the instruction using a recognized Python form, such as a print call with quoted text.

  • Common mistake

    Assuming Python will repair almost-correct syntax.

    What it looks like

    A programmer omits one character and expects Python to infer the missing part.

    Why it fails

    Python is strict about syntax and does not guess the programmer's intent.

    Do this instead

    Use the error message to find the missing or misplaced part and correct it explicitly.

Try the Repair

EASY

Imagine that Python rejects a print call because its text begins with a quotation mark but does not end with one. Describe the correction cycle in order: what information would you read first, what part of the line would you inspect, and what would you change before trying again?

Hints
  • Start with the line number, error name, and description.
  • Inspect the quotation marks on the reported line.
  • Look for an opening quote without a matching closing quote.

What do you think happens?

What is the most useful first response when Python reports a SyntaxError?

  • Assume Python will understand the intended meaning.

  • Read the line number and error description, then inspect the reported code.

  • Replace the entire program without reading the message.

Reveal answer
Answer:

Read the line number and error description, then inspect the reported code.

The error message supplies the location and description needed to identify the syntax violation and make a focused correction.

Working Rule

Calling a function requires exact syntax. For the print function, that means the function name, parentheses, and quoted text in the expected arrangement. If Python rejects the line, treat the SyntaxError as information: locate the line, read the pointer and description, identify the rule you violated, correct the code, and try again.

Key point

Python does not guess intent. Correct syntax is the way the programmer communicates instructions that Python can execute.

Key Takeaways

  • Python enforces exact syntax and rejects code that violates its rules, even by one character.
  • A print function call uses the word print, parentheses, and text enclosed in matching quotation marks.
  • A SyntaxError provides a line number, the relevant code, a pointer to the detected location, an error name, and a description.
  • Python cannot infer what you meant from natural language or almost-correct syntax.
  • The normal learning cycle is to write code, read the error, identify the problem, correct it, and try again.

Related concepts

More concepts starting with F