Introduction to Computer Science/4.8 Code Tracing & Scope Lab

4.8 Code Tracing & Scope Lab

🎯 Assessment Goals
  • Demonstrate your ability to trace the flow of execution through multiple function calls.
  • Predict the final output of a complete program.
  • Correctly identify the values of local and global variables at specific points in the program.

Minor Assessment: Code Tracing & Scope Lab (5%)

Welcome, code detective! An essential skill for any programmer is the ability to read a piece of code and mentally "run" it to predict its behavior. This is called code tracing. It is the single most important skill for finding and fixing bugs.

Your Task

Carefully read the Python script in the image below. Do not run this code in an editor. The goal is to trace its execution manually. In a separate document, answer the five questions that follow.

The Code to Analyze:

The Questions to Answer:

  1. What is the very first line of output that will be printed to the screen when this program runs?
  2. What is the value printed for local_x inside function_a?
  3. What is the value printed for result back in the main function?
  4. What is the value printed for the local x at the end of the main function?
  5. What is the final value of the global x that is printed on the very last line?

Submission

Submit a document (e.g., Google Doc, Word file, or PDF) with your numbered answers to the "Unit 4 Code Tracing Lab" Assignment folder in D2L.

Strategy for Success: The Variable Table

Get a piece of paper and make a table with two columns: "Global Scope" and "main() Scope". As you trace the code line by line, write down the variables in each column and update their values. This is the best way to keep track of everything!