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.

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

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.

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.

continuepress Enter twicedefinition endsfunction headerdef line ending with :indented bodyfunction linesempty lineno indentation or code>>>ready for a new command
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.

after headercompletion signalinterpreter reads structurefunction endsinteractive modeone line at a timescript fileentire file available. . .waiting for completionindentation changereveals the functionboundaryempty lineends definitionnext lineno empty line required
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.

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

Defining and Testing a Function

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

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.

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

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.

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.

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

  • Treating . . . as an error message.

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

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

  • Entering the body without indentation.

    Indentation tells Python which lines belong to the function.

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

  • Trying to finish the definition with a nonempty line.

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

    Fix: Press Enter again without typing anything.

  • Expecting a script file to require the same empty line.

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

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

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

Key Takeaways

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