# Add exercise to stress how functions are different from math functions #489

Open
opened this Issue Mar 20, 2018 · 6 comments

Projects
None yet
4 participants
Contributor

### iglpdc commented Mar 20, 2018 • edited by maxim-belkin

 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.
Collaborator

### annefou commented Mar 27, 2018

 Our lessons are using python 3 and in python 3 print() is a function and not a statement.

Collaborator

### maxim-belkin commented Mar 28, 2018

 @annefou I think there are two points here: two calls to print and print vs. return. def one(): print(1) print(5) a = one() # what is the value of `a`? It would be interesting to hear what other people think :)
Collaborator

### annefou commented Mar 28, 2018

 a will be None because the function one does not return a value. Same for print. If there is no return statement in the function code, the function ends, when the control flow reaches the end of the function body and the value "None" will be returned.
Collaborator

### maxim-belkin commented Mar 28, 2018

 :) I know

Collaborator

### maxim-belkin commented Apr 4, 2018

 Hmmm... I think it is worth comparing two functions with and without the return statement: print compute_income1(salary): print(salary*0.7) versus print compute_income2(salary): return salary*0.7

Merged

Contributor

### iglpdc commented May 5, 2018 • edited

 I think the point of Mark's exercise is not so much distinguishing return vs print, but the fact that people with a mathematical background tend to assume that functions in programming are equivalent to mathematical functions. A mathematical function will map an input to an output, i.e. get some input argument and return a value (None counts as a value here). In the exercise, people may follow this trap and say that there is only one number being printed. I think not using return and instead using print twice stresses the issue. A function returns thru one return line every time is executed, so it looks like a mathematical function, but in cases like the above we see that is actually a set of statements. I think @maxim-belkin 's exercise is too "easy" :) def one(): print(1) print(5) a = one() # what is the value of `a`? Also the value of a is not the point, in the sense that a can have only one value. The point is that two numbers are printed (I guess most people would say that here and that's why I'm saying it's easy). But in Mark's example, you start doing the math and think that the important thing is that the value of salary changes, you only keep track of the updated value and because it really looks like a mathematical function, you may miss the first print(salary) if you have the (wrong) preconception that functions are mathematical functions. @annefou's response might reinforce the wrong preconception: a will be None because the function one does not return a value. Same for print. If there is no return statement in the function code, the function ends, when the control flow reaches the end of the function body and the value "None" will be returned. 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.