Concepts / Defining and Calling Functions

Defining and Calling Functions

The raw_input() function is the gateway for this process.

  • Programming

From Keyboard to Variable

A user response does not appear in a program all at once. It travels through a sequence: the program displays a prompt, waits for input, receives the text after the user presses Enter, and stores the returned string in a variable. The raw_input() function is the gateway for this process.

Keyboardtyped textraw_input()captures textStringreturned textsomethingstored stringuser types and presses Enterreturnsstored in
How does text move from the keyboard through raw_input() and become a string stored in a variable?
Definition

raw_input() captures text from the keyboard and returns it as a string to your program.

Worked example

Storing a Response

Follow what happens when a program uses something = raw_input("Enter text: ").

  1. 1
    Display

    The prompt string Enter text: is displayed.

  2. 2
    Wait

    The program waits for the user to type and press Enter.

  3. 3
    Capture

    The typed text is captured and returned by raw_input().

  4. 4
    Store

    The returned text is stored in the variable something as a string.

Result

The variable something now contains the user's typed text, ready for processing.

Following the Response

After raw_input() returns text, the program can process the value and produce an output. The complete path has three important stages: input capture, data processing, and output. Keeping these stages separate makes it easier to determine where an unexpected result began.

Promptdisplay requestInput captureraw_input()Stored stringvariable valueProcessingprogram logicOutputresultuser respondsreturns textpasses valueproduces result
What happens next to the user's response as the program receives it, stores it, and applies logic?
ProgramUserVariablestored stringProgram logicresulting actiondisplays prompttypes responsestores returned stringpasses response
How are the prompt, the user's response, the stored string, and the resulting action connected?

Definitions and Calls

A function definition describes a reusable piece of program logic. A function call uses that function so its logic can be applied to a value. In the source example, reverse() is defined to flip a string backwards, and is_palindrome() is defined to compare original text with its reversed version. The main program calls is_palindrome() after it captures the user's text.

defstarts a definitionreversefunction namereverse logicfunction bodyreverse(text)function callreversed textreturned resultnamescontainsexecutesproduces
What is the difference between defining a function and calling it, and when does the function's code execute?
Code walkthrough
def reverse(text):
    return text[::-1]

def is_palindrome(text):
    return text == reverse(text)

something = raw_input("Enter text: ")
result = is_palindrome(something)
print result
  1. 1
    The program defines reverse() before using it.
  2. 2
    The program defines is_palindrome(), which uses reverse() as part of its comparison.
  3. 3
    The program calls raw_input() and stores the returned string in something.
  4. 4
    The program calls is_palindrome(something), passing the stored text into the checking logic.
  5. 5
    The result is printed after the input has been processed.
  • The reverse() definition describes logic for flipping a string backwards.
  • The is_palindrome() definition compares the original text with the result from reverse().
  • The raw_input() call captures the user's text and stores it in something.
  • The is_palindrome() call applies the checking logic to something.
  • The final statement prints the result.

Palindrome Checker

A palindrome is a word or phrase that reads the same forwards and backwards. The complete program uses two helper functions: reverse() flips the text backwards, and is_palindrome() compares the original text with the reversed text. The main program captures input, passes it to the checking function, and prints the result.

Worked example

Tracing the Palindrome Program

Follow the response through the program when the user enters a palindrome.

  1. 1
    Input capture

    raw_input() displays the prompt, waits, and returns the user's text as a string.

  2. 2
    Function call

    The stored string is passed to is_palindrome().

  3. 3
    Reverse

    is_palindrome() uses reverse() to create the backwards version of the text.

  4. 4
    Comparison

    The original string is compared with its reversed version.

  5. 5
    Output

    The comparison result is printed.

Result

The program's output depends on whether the original text and reversed text match.

This example illustrates why definitions and calls must be traced separately. The helper functions describe the processing steps, but the processing is used only when the main program calls the appropriate function with the captured input.

Finding the Divergence

When the output is unexpected, do not inspect only the final line. Trace the state of the data step by step. Ask whether the prompt and capture happened as expected, whether the stored string is what you intended, whether the processing logic handled that string correctly, and whether the output reflects the processing result.

levelexpected inputlevelcaptured string includeswhitespacepalindromeexpected resultnot palindromeunexpected resultprocessingprocessing
Where does the actual input-processing path diverge from the expected result?
  • Common mistake

    Treating the value returned by raw_input() as a number automatically.

    What it looks like

    A value captured with raw_input() is used directly for numeric operations.

    Why it fails

    raw_input() returns a string.

    Do this instead

    Convert the returned value to int or float when numeric operations are needed.

  • Common mistake

    Assuming the captured text is exactly the text the program expected.

    What it looks like

    A palindrome check produces an unexpected result even though the visible word appears correct.

    Why it fails

    Whitespace and case sensitivity can affect how text is processed.

    Do this instead

    Trace and inspect the input-processing path, accounting for whitespace and case.

  • Common mistake

    Debugging only the final output.

    What it looks like

    The result is wrong, but the input capture and processing stages are not checked.

    Why it fails

    The divergence may have occurred during input capture, processing, or output.

    Do this instead

    Trace execution step by step and examine the data state at each stage.

Best practice

Use the three-stage model when debugging: input capture, palindrome checking or other processing, and output. Identify the value at each stage before deciding which part of the program needs attention.

Trace It Yourself

MEDIUM

Trace a palindrome-checking program from the moment its prompt appears until its result is printed. For each stage, write down what happens to the user's text: capture, storage, processing, and output. Then explain what you would inspect first if the result did not match your expectation.

Hints
  • Begin with the fact that raw_input() returns a string.
  • Separate the stored input from the work performed by the helper functions.
  • Check whether the divergence occurred during input capture, processing, or output.
  • What prompt does the program display?
  • What string does raw_input() return?
  • Which variable stores that string?
  • Which function receives the stored string?
  • What processing does the function perform?
  • What value is finally printed?

Key Takeaways

  • raw_input() displays a prompt, waits for keyboard input, and returns the typed text as a string.
  • The returned string is stored in a variable and can then be passed into program logic.
  • A function definition describes processing logic, while a function call applies that logic to a value.
  • The palindrome example captures text, reverses it, compares the two versions, and prints the result.
  • To debug unexpected output, trace input capture, processing, and output step by step.

Related concepts

More concepts starting with D