Concepts / Defining and Calling Functions

Defining and Calling Functions

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

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

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

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

Storing a Response

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

Display: The prompt string Enter text: is displayed.

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

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

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

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.

user respondsreturns textpasses valueproduces resultPromptdisplay requestInput captureraw_input()Stored stringvariable valueProcessingprogram logicOutputresult
What happens next to the user's response as the program receives it, stores it, and applies logic?

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.

namescontainsexecutesproducesdefstarts a definitionreversefunction namereverse logicfunction bodyreverse(text)function callreversed textreturned result
What is the difference between defining a function and calling it, and when does the function's code execute?

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

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.

Tracing the Palindrome Program

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

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

Function call: The stored string is passed to is_palindrome().

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

Comparison: The original string is compared with its reversed version.

Output: The comparison result is printed.

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.

processingprocessinglevelexpected inputlevelcaptured string includeswhitespacepalindromeexpected resultnot palindromeunexpected result
Where does the actual input-processing path diverge from the expected result?
  • Treating the value returned by raw_input() as a number automatically.

    raw_input() returns a string.

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

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

    Whitespace and case sensitivity can affect how text is processed.

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

  • Debugging only the final output.

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

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

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.