source-academy / cs1101s

1 stars 0 forks source link

How to use substitution model to evaluate a program involving assignment statement? #12

Open mrqorib opened 3 years ago

mrqorib commented 3 years ago

Question: When we use substitution model to evaluate a program involving assignment statement, I'm not quite sure when to substitute. For example in the case below, when we pass an argument 25 to the parameter balance, then in the "balance = balance - amount" part, only the second occurrence of balance is replaced by 25, while the first one doesn't change. Why is this the case? What is a general rule of replacing the parameter with the actual argument? image image

Piazza: https://piazza.com/class/kas136yscf8605?cid=520

mrqorib commented 3 years ago

Answer: The central idea of Lecture L7 was that the substitution model breaks down when you have assignments. In other words, you should not and cannot use the model for programs that have assignments.