Concepts / Understanding Function Syntax

Understanding Function Syntax

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

  • Programming

The Waiting Prompt

When you define a function in an interactive interpreter, the interpreter cannot finish processing the definition after the first line. The function header announces that a body is coming, so the interpreter changes its prompt. Instead of returning to the normal >>> prompt, it displays . . . . This is not an error message. It is a signal that the definition is incomplete and that the interpreter is waiting for the function body.

enter function headerpress Enterenter indented lines>>> promptready for inputdef lineends with a colon. . . promptfunction body expectedindented bodylines belong to thefunction
What changes after the function header is entered, and what does the ellipsis prompt mean?

Reading the Ellipses

The . . . prompt tells you that the interpreter is still collecting the function definition. After the function header, enter the function body at the ellipsis prompt. Every line that belongs to the body must be indented. Typically, indentation uses four spaces or one tab. If the body has more than one line, each line that belongs to it must use the same indentation level.

>>> def greet(): ... print("Hello") ...

Closing the Definition

After entering the final indented body line, the interpreter still needs a clear signal that the function body has ended. In interactive mode, provide that signal by entering an empty line: press Enter to move to a new line, then press Enter again without typing code or indentation. The blank line tells the interpreter that the definition is complete, so it returns to the normal >>> prompt.

press Enterenter indented linepress Enter twiceinterpreter closes definitiondef lineheader entered. . . promptbody expectedindented bodyfunction statementsempty lineno code, no indentation>>> promptdefinition complete
How does the interpreter know that the function body is finished?
python
Output (expected)
Hello

Defining and testing greet

Complete a function definition in the interactive interpreter and then call the function.

Enter the header: The function header is entered at the >>> prompt. After Enter, the interpreter displays . . . because the definition is incomplete.

Enter the body: Enter the body line at the . . . prompt with indentation. The indentation identifies the line as part of the function.

Enter the empty line: Press Enter again on a blank line with no indentation and no code. This ends the definition and returns the >>> prompt.

Call the function: At the returned >>> prompt, enter the function call to test the completed definition.

The function definition is complete, and calling greet produces Hello.

Interactive Sessions and Scripts

The empty-line requirement belongs to interactive mode. In an interactive session, the interpreter receives one line at a time, so it needs a signal that no more lines belong to the current function definition. The . . . prompt shows that the interpreter is waiting, and the empty line signals completion. In a script file, the interpreter can read the file structure and recognize that the function has ended when indentation returns to the base level. A script therefore does not need a separate empty line to terminate the function.

continues definitionusesreads structureInteractive modeone line at a time. . . promptdefinition waitingempty linecompletion signalScript filewhole file availablebase indentationfunction end
Why does interactive mode need an empty line while a script can continue using its file structure?
Interactive modeScript file
Displays . . . while the function definition is incomplete.Does not need to display . . . while reading the file.
Requires an empty line to signal that the definition is complete.Uses the file structure and indentation changes to determine where the definition ends.
Receives input one line at a time.Can read the entire file at once.

REPL Practice

EASY

In an interactive interpreter, define a function named welcome with one indented body line that displays Welcome. Then finish the definition correctly and call welcome to test it.

Hints
  • Begin the header at the >>> prompt.
  • When . . . appears, enter the body with indentation.
  • After the body line, press Enter again on a completely blank line.
  • Call the function only after the >>> prompt returns.

What do you think happens?

After entering an indented body line, what should you do to finish the function definition in interactive mode?

  • Enter another indented statement forever
  • Press Enter on a blank line with no code or indentation
  • Return to the >>> prompt manually by typing it
Reveal answer

Answer: Press Enter on a blank line with no code or indentation.

The empty line is the interactive-mode signal that the function body is complete. The interpreter then returns to the normal >>> prompt.

Frequent Prompt Mistakes

  • Treating the . . . prompt as an error.

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

    Fix: Enter the function body at the . . . prompt.

  • Entering a body line without indentation.

    Indentation tells the interpreter which lines belong to the function body.

    Fix: Indent each body line, typically by four spaces or one tab.

  • Typing a new statement instead of an empty line after the body.

    The interpreter is still collecting the function definition, so the definition has not yet been terminated.

    Fix: Press Enter again without typing code or indentation, then wait for the >>> prompt.

  • Expecting a script file to require the same blank-line action.

    The empty-line requirement is specific to interactive mode. A script's indentation and file structure show where the function ends.

    Fix: Use the empty line when entering the definition interactively; do not treat it as a required script-file terminator.

Use the prompt as a progress indicator: >>> means the interpreter is ready for a new statement, while . . . means the current function definition is still being collected. This simple habit makes interactive function testing more predictable.

Key Takeaways

  1. The . . . prompt means that an interactive function definition is incomplete and is waiting for its body.
  2. Function body lines must be indented so the interpreter knows they belong to the function.
  3. Press Enter twice after the final body line, with the second line completely empty, to finish the definition interactively.
  4. Interactive mode needs this blank-line signal because it receives input one line at a time.
  5. A script file can determine the function's end from its complete structure and indentation changes, so it does not require the interactive blank-line action.

Key Takeaways

  • The . . . prompt signals an unfinished interactive function definition.
  • Indent the function body consistently after the ellipsis prompt.
  • End the interactive definition with a completely empty line.
  • The blank-line rule is needed in interactive mode, not in script files.
  • After the >>> prompt returns, the function is ready to test.