Following local copies through helper calls

A student tests an inventory helper for an Arduino parts dispenser. The helper returns the number of parts left after a requested removal, limiting the removal to the available stock. Trace the sketch exactly as written:


C++
int removeParts(int stock, int amount) {
  if (amount > stock) {
    amount = stock;
  }
  stock = stock - amount;
  return stock;
}

void setup() {
  int stock = 23;
  int afterFirst = removeParts(stock, 8);
  int afterSecond = removeParts(afterFirst, 19);
  int finalStock = removeParts(stock, afterFirst);
}

void loop() {
}


Immediately after the assignment to finalStock, while the variables are still in scope, what values do afterFirst, afterSecond, finalStock and the stock declared inside setup() contain? Enter each quantity in parts as an exact integer.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Arduino Programming Questions: