← Introduction to Computer Science

Unit 6: Functions, Scope, and the Call Stack

Why code gets cut into named pieces, where a variable can be seen from, and the stack of frames that makes it all work.

7 topics · 28 questions

Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.

Why Factor Code into Functions

  1. How it worksHow does wrapping repeated code in a function let you fix a bug in exactly one place?
  2. Common mistakeWhy isn't a function just a way to make a program file shorter to look at?
  3. Going furtherWhat happens to a program's bug count when the same logic is duplicated in six different spots?
Your turn1 of 2
The same 5 lines of logic are copied into 15 different places in a program instead of being wrapped in one function. If a bug is later found in that logic, how many separate places need to be fixed under the copy-paste approach?

Parameters, Arguments, and Return Values

  1. How it worksHow does calling square(4) copy the value 4 into the parameter n before the function body runs?
  2. Worked exampleTrace def square(n): return n·n called as square(6).
  3. Common mistakeWhy doesn't printing inside a function count as the same thing as returning a value?
  4. Going furtherWhat happens when a function has no return statement at all and its result gets used anyway?
Your turn1 of 2
A function square(n) returns n·n. What value does square(35) return?

Local Versus Global Scope

  1. How it worksHow does a local variable live only inside the function that created it while a global one lives everywhere?
  2. Worked exampleTrace what happens when a function reads a global total but a same-named local total sits below it.
  3. Common mistakeWhy doesn't changing a local variable inside a function change a global variable with the same name?
  4. Going furtherWhat changes once a function explicitly declares a variable global before assigning to it?
Your turn1 of 2
A function has 11 local variables and can also read 10 global variables. How many total variables, local plus global, can the function's code reference?

The Call Stack

  1. How it worksHow does calling a function push a new frame onto the call stack holding its own local variables?
  2. Worked exampleTrace the call stack's depth as main() calls first(), first() calls second(), and second() returns.
  3. Common mistakeWhy isn't the call stack just a list of which functions have been called somewhere in the program?
  4. Worked exampleTrace the call stack's maximum depth for three nested calls each waiting on the one below to return.
  5. Going furtherWhat happens to the call stack once one of those three functions calls itself instead of the next one?
Your turn1 of 2
A chain of 11 nested function calls, each waiting on the next to return, reaches its deepest point once the innermost function starts running. Counting the very first calling function as depth 1, what is the call stack's depth at that deepest point?

Default Parameter Values

  1. How it worksHow does a default value fill in a parameter automatically when the caller leaves it out?
  2. Worked exampleTrace def greet(name, greeting='Hello'): return greeting + name called as greet('Ana').
  3. Going furtherWhat happens when a caller supplies a value for a parameter that already has a default?
Your turn1 of 2
A function greet(name, greeting='Hello') is called with only the name argument supplied, letting greeting fall back to its default value of 5 characters. If the name argument itself is 21 characters long, how many total characters does greeting concatenated with name produce?

How Arguments Actually Get Passed

  1. How it worksHow does passing a number copy its value while passing a list copies only the reference to it?
  2. Worked exampleTrace a function that reassigns its parameter to 99 versus one that appends to a list parameter, on x=5 and lst=[1,2].
  3. Common mistakeWhy doesn't passing an object into a function make a full copy of that object?
  4. Going furtherWhat happens to the caller's list once a function passed that list clears it instead of reassigning it?
  5. Going furtherWhat changes once a function reassigns the list parameter to a brand new list instead of mutating it?
Your turn1 of 2
A function reassigns its parameter to 70, but this has no effect on the caller's original variable, which still holds its original value of 65. What value does the caller's original variable hold after the function call returns?

Pure Functions Versus Side Effects

  1. How it worksHow does a pure function avoid touching any variable or file outside its own parameters?
  2. Worked exampleTrace calling a pure add(a, b) three times with the same arguments and compare it to a counter with a side effect.
  3. Common mistakeWhy isn't a function pure just because it never modifies one of its own parameters?
  4. Ties togetherHow does a pure function make it safe to memoize its results the way recursive Fibonacci does?
Your turn1 of 2
A pure function add(a, b) is called 70 times with the exact same two arguments each time. How many of the n calls return the exact same result?