Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Add exercise to stress how functions are different from math functions #489
Mark Guzdial had an interesting post a few days ago on how learners with a math background may assume mistakenly that a function in programming is the same as a function in math. The exercise just asks the learner what's the output of:
def computeSalary(): rate = 10.00 hours = 40 salary = hours * rate hours = 30 print salary increase = 1.00 salary = hours * (rate + increase) print salary
I tried it and got it wrong and realized that, in fact, I've always thought in functions as mathematical functions.
As we have many learners from science and math camps, I think it would be a good addition to the episode on functions.
I think the point of Mark's exercise is not so much distinguishing
In the exercise, people may follow this trap and say that there is only one number being printed. I think not using
I think @maxim-belkin 's exercise is too "easy" :)
Also the value of
But in Mark's example, you start doing the math and think that the important thing is that the value of
@annefou's response might reinforce the wrong preconception:
If I have the wrong idea about functions, I'm still confortable with this response because it's focusing in the point that functions take something and return something else.
To summarize, I'd say that the point of the exercise is to stress that despite functions can return things and have only one exit point (so they return one thing), they can execute arbitrary code and should not be thought of as mathematical functions.