Concepts / Writing Functions in Script Files

Writing Functions in Script Files

In interactive mode, the interpreter displays ellipses ( . . . ) to indicate that a function definition is incomplete and waiting for the function body.

  • Programming

The Prompt Changes

When you define a function in Python's interactive mode, the interpreter does not treat the function header as a complete statement. After you enter a line beginning with def and ending with a colon, the normal >>> prompt changes to . . . . This change tells you that the interpreter is waiting for the function body and that the definition is not complete yet.

>>>normal promptfunction headerdef line ending with :. . .function definitionincompletefunction bodyindented linesenterinterpreter waitsprovide
What changes after the function header is entered, and what does the ellipsis prompt communicate?
Definition

In interactive mode, the ellipses prompt, written as . . . , indicates that a function definition is incomplete and that the interpreter is waiting for the function body.

Building the Definition

The ellipses are not an error message. They are a signal that the interpreter has recognized the start of a multi-line function definition and is collecting the lines that belong to it. The body must be indented. Typically, the indentation is four spaces or one tab, and every line that belongs to the body must use the same indentation level.

In the real world

Generated example: Imagine defining a function named greet in the REPL. After entering its function header, you see . . . rather than >>>. You then enter an indented body line. Seeing . . . again means the interpreter is still collecting the definition, not that the function has already been completed.

function headerdef line ending with :indented bodyfunction linesempty lineno indentation or code>>>ready for a new commandcontinuepress Enter twicedefinition ends
How does an empty line move the interpreter from collecting a function to accepting the completed definition?

Interactive Mode and Scripts

Interactive mode receives one line at a time, so the interpreter needs a clear signal that the multi-line function definition is finished. The . . . prompt signals that the definition is still being collected, and an empty line signals that collection should end.

A script file works differently. The interpreter can read the entire file and use indentation changes to determine where the function ends. When a line returns to the base indentation level with no indentation, the interpreter knows that the function definition has ended. Therefore, a script file does not require the special empty line used in interactive mode, and it does not display ellipses as interactive feedback.

interactive modeone line at a timescript fileentire file available. . .waiting for completionindentation changereveals the functionboundaryempty lineends definitionnext lineno empty line requiredafter headercompletion signalinterpreter reads structurefunction ends
Why does interactive mode need an empty line while a script file can continue to the next line?

A REPL Testing Routine

A reliable interactive workflow has four stages. First, enter the function header. Second, enter each body line with the required indentation. Third, enter an empty line to finish the definition and return to the >>> prompt. Fourth, call the function to test it. The return to >>> is the useful confirmation that the interpreter has accepted the definition and is ready for a new command.

function headerenter def lineindented bodyprovide function linesempty linepress Enter twice>>>definition acceptedfunction calltest the functioncontinuefinish bodyreturnenter test
What is the complete REPL flow from entering a function header to testing the completed function?
Worked example

Defining and Testing a Function

Use the REPL to define a function and then test it.

  1. 1
    Enter the header

    At the normal >>> prompt, enter a function definition header ending with a colon. The interpreter changes to the . . . prompt because the definition is incomplete.

  2. 2
    Enter the body

    At the . . . prompt, enter the function body with indentation. A further . . . prompt indicates that the interpreter is still inside the definition.

  3. 3
    End the definition

    Press Enter to move to a new line, then press Enter again without entering code or indentation. This empty line tells the interpreter that the definition is complete.

  4. 4
    Test the function

    Wait for the normal >>> prompt to return, then enter a call to the function. This lets you test the definition immediately in interactive mode.

Result

The function is defined and ready to be called after the interpreter returns to the >>> prompt.

What do you think happens?

After entering the final indented body line, what should you do before entering a function call?

  • Enter the function call immediately at the . . . prompt.

  • Enter an empty line with no indentation or code.

  • Indent the function call as another body line.

Reveal answer
Answer:

Enter an empty line with no indentation or code.

The empty line ends the interactive function definition. The interpreter then returns to the normal >>> prompt, where a new command such as a function call can be entered.

Frequent REPL Mistakes

  • Common mistake

    Treating . . . as an error message.

    What it looks like

    The interpreter displays . . . after the function header.

    Why it fails

    The ellipses indicate that the function definition is incomplete and that the interpreter is waiting for the body.

    Do this instead

    Enter the indented function body, then finish with an empty line.

  • Common mistake

    Entering the body without indentation.

    What it looks like

    A body line is entered at the base indentation level.

    Why it fails

    Indentation tells Python which lines belong to the function.

    Do this instead

    Indent each body line, typically by four spaces or one tab, and keep subsequent body lines at the same indentation level.

  • Common mistake

    Trying to finish the definition with a nonempty line.

    What it looks like

    Text or code is entered on the line intended to end the function.

    Why it fails

    The completion signal is a completely blank line with no indentation and no code.

    Do this instead

    Press Enter again without typing anything.

  • Common mistake

    Expecting a script file to require the same empty line.

    What it looks like

    A learner adds an empty line specifically to tell a script that the function has ended.

    Why it fails

    The interpreter can read the entire script and infer the boundary from indentation changes.

    Do this instead

    In a script, the function ends when the following line returns to the base indentation level.

Best practice

Use the prompt as a progress indicator. The . . . prompt means keep supplying the function definition. The >>> prompt means the interpreter is ready for a new command, such as a function call.

Practice the Transition

EASY

Describe the REPL steps you would use to define a function with one indented body line and then test it. Include what the . . . prompt means and explain exactly how you would return to the >>> prompt.

Hints
  • Start with the function header at the >>> prompt.
  • The body line must be indented.
  • The definition ends with a completely blank line.
  • Only after the >>> prompt returns should you enter the function call.

Key Takeaways

  1. 1
    The . . . prompt means an interactive function definition is incomplete.
  2. 2
    The function body must be indented so the interpreter knows which lines belong to the function.
  3. 3
    Press Enter twice, with the second line completely blank, to end a function definition in interactive mode.
  4. 4
    Interactive mode needs this empty-line signal because it receives input one line at a time.
  5. 5
    Script files do not require the empty line because indentation changes reveal where the function ends.
  6. 6
    After the interpreter returns to >>>, you can call the function to test it.

At a glance

  • The . . . prompt shows that the interpreter is waiting for the rest of a function definition.
  • Indent every line in the function body.
  • End an interactive definition with a completely blank line by pressing Enter twice.
  • Scripts use their file structure and indentation changes to identify the end of a function, so they do not need this interactive empty-line step.
  • Test the function only after the normal >>> prompt returns.

More concepts starting with W