Answers.Body: <p>Your <em>specific</em> question is whether there have been any studies of whether it is more effective to:</p>
  
  <ul>
  <li>Introduce mathematical functions first, then explain how computer program functions can be nondeterministic or have side effects, or</li>
  <li>Introduce computer programming functions first, then explain how mathematical functions are a disciplined subset of those that have a fixed deterministic mapping.</li>
  </ul>
  
  <p>I am not aware of any research on this question. My intuition is that it would be difficult to run such an experiment, because students encounter the mathematical notion of function when they are twelve years old. <strong>I've taught since the 80's and on two continents, and have never seen a beginning programming student who hadn't already seen mathematical functions first</strong>.</p>
  
  <p>Your <em>implied</em> question is how best to teach the difference. In my experience, beginning programmers quickly learn that we redefine (or misuse) lots of words from other fields, and don't get hung up on vocabulary.</p>
  
  <p>Again in my experience, the distinction that seems to help is differentiating between <em>nondeterminism</em> and <em>side effects</em>. Your example of <code>rand</code> falls in the former case: it looks like a function, it has a domain and a range, but it doesn't always give the same answer. That concept is pretty specific to CS and is (as they say) a good pedagogical opportunity.</p>
  
  <p>When the concept of side effect is introduced it is usually best to be explicit about which object is maintaining the state, and call the "function" a <em>method</em>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-17T12:17:45.180Z
Answers.Text: What levels did you teach? I wrote my first functions in Logo in primary school, and I didn't learn mathematical functions until high school. Surely I can't be the only one. And students _do_ have a difficulty with math functions that doesn't apply to computer functions, namely, the fact that in math, different algorithms that compute the same result for every input are the same function.
Answers.UserDisplayName: Gilles 'SO- stop being evil'
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-17T19:45:14.690Z
Answers.Text: Perhaps you didn't see functions formally, but you were intimately acquainted with the fact that `2 + 2` is always `4`.
Answers.UserDisplayName: vonbrand
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-16T13:17:39.857Z
Answers.LastActivityDate: 2018-05-16T13:17:39.857Z
Answers.UserDisplayName: djs
Answers.Body: <p>A bit of history may help: the (now confusing) word <code>FUNCTION</code> was introduced in FORTRAN II (1958, see manual <a href="http://archive.computerhistory.org/resources/text/Fortran/102653989.05.01.acc.pdf" rel="nofollow noreferrer">here</a>), together with SUBROUTINE as facilities in order to "effectively enable the programmer to expand the language of the system indefinitely".</p>
  
  <p>In the original version of FORTRAN, the programmer simply used formulaes, which were "a kind of arithmetic statements" (P10), under the form of a function name followed by arguments enclosed in parentheses etc.</p>
  
  <pre><code>FIRSTF(X) = A*X + B
  </code></pre>
  
  <p>and were clearly intended (at least, if you the formula doesn't refer to functions with side-effects) to be side-effect free.</p>
  
  <p>The new FUNCTION device was here to allow more complex functions with several statements and auxiliary variables. Also for separate compilation and code reuse. Opened the door to use FUNCTION for code units that do some processing, possibly with side-effects, and incidentally return some value. Say, an error code ?</p>
  
  <p>The distinction between FUNCTION and SUBROUTINE was later blurred with C. Everybody is a so-called function now. Not necessarily related to the mathematical notion of functions.</p>
  
  <p>In my current way of teaching to novice programmers, I first introduce "void functions/methods" without parameters as groups of actions. The use of the Processing environment helps here as it is easy to give the example of a big drawing with parts defined in methods for obvious convenience.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-17T07:12:40.480Z
Answers.LastActivityDate: 2018-05-17T20:49:41.370Z
Answers.LastEditDate: 2018-05-17T20:49:41.370Z
Answers.UserDisplayName: Michel Billaud
Answers.Body: <p>In mathematics a function is a constrained relation between two sets. You can illustrate that in a number of ways. The constraint implies consistence of operation and uniqueness of result. </p>
  
  <p>If you discuss relations (in math) as well as functions it may be easier to get beginning students to grok it. </p>
  
  <p>In most computing languages, a function is an operation that produces a value (and possibly side effects - horror). There is nothing in the definition of a Java function about constraint, not even that successive invocations with the same input produce the same values. A java function is a machine. Drop in 0 or more inputs, turn the crank, get some outputs. You can illustrate that by drawing a machine with an input hopper, and output spigot and a crank. </p>
  
  <p>Students who know math have a lot of problems early on with things that look like math but are not. Other questions in the "hopper" currently explore other aspects (equality, variables)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T18:24:23.660Z
Answers.Text: uniqueness of result is not implied. In fact, just plot $x = 16y^2$ and you'll see multi-valued results.
Answers.UserDisplayName: ItamarG3
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T20:00:55.660Z
Answers.Text: I was writing about functions. Your example is an equation (a relation). $f(y) = 16y^2$ is single valued. But yes, the equation has multiple solutions. That isn't the same thing. And of course, the unique result (range value) of a function could be a set. But still unique. Am I missing something? (deleted and re-entered, since I can't edit my earlier error.)
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-02T09:21:24.180Z
Answers.Text: @nocomprende, I think that is a good thing. In fact, I think that early CS education puts far too much stress on mathematical examples. That seems to be a consequence of trying to teach the primitives (int, float, ...) earlier than they need to be taught. There are exceptions, of course.
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T18:21:53.200Z
Answers.LastActivityDate: 2017-07-01T18:21:53.200Z
Answers.UserDisplayName: Buffy
Answers.Body: <p>A function in mathematics is a "mapping" between one set and another set (in the non-strict definition, I think it suffices for this discussion). For example, a function $f: X \rightarrow Y$ where $X = \{a, b\}$ and $Y = \{1, 2\}$ could very well just be that $a = 1$ and $b = 2$. The key thing is you take one value $x\in X$ and you get <em>one</em> value $y\in Y$ (there are other constraints, but I have no wish to double check them or go into an in-depth mathematical discussion here). </p>
  
  <p>A function in computer science (at least the ones I'm used to, in for example Python) are just a set of commands that can take some inputs, produce some outputs, and do some things along the way. For example, the Python equivalent of $f$ above would be </p>
  
  <pre><code>def someFunction(x):
      if x == 'a':
          return 1
      elif x == 'b':
          return 2
      else:
          return 'Stop messing with this pseudocode example, will ya?'
  </code></pre>
  
  <p>But another function could be </p>
  
  <pre><code>def anotherFunction(x, y, z):
      localVar = x+y
      list = []
      list.append(localVar)
      print("this function does all the things")
      return sum(list) + z
  </code></pre>
  
  <p>The above, while a pretty useless function (I'm terrible at thinking up functions with no purpose =P), does a lot more things than a function in math does. It does some things on the side, returns some stuff, takes multiple inputs and outputs, and so on. It's a complicated beast. Heck, you can even write a function like</p>
  
  <pre><code>def yetAnotherFunction():
      print('yay')
  </code></pre>
  
  <p>That doesn't take any inputs or return anything. </p>
  
  <p>I guess, what I'm saying is - I think the two are so different, I'm not really sure there's a ton to be gained by exploring the contrast. A function in computer science is a module of code that can be called, perhaps with inputs, and does some stuff, maybe returning something. A function in math is a very strictly defined thing: you put something in, and you get only one thing out.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-17T12:20:39.153Z
Answers.Text: What you're calling a “function in computer science”, I would call a “function in programming”. Functions in computer _science_ can get a lot more subtle, but that's something you'd teach only in a semantics course, not in a programming course.
Answers.UserDisplayName: Gilles 'SO- stop being evil'
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-17T12:25:59.923Z
Answers.Text: While you may start by saying that the word “function” means unrelated things in math and programming, that's only the start. There are both mathematical and cognitive associations between the two concepts. It would depend on the level and the interests of the students, but I think the relationship does need to be discussed at some point.
Answers.UserDisplayName: Gilles 'SO- stop being evil'
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-17T19:43:32.967Z
Answers.Text: Don't forget the (not at all) subtle difference in what "variable" means, either...
Answers.UserDisplayName: vonbrand
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-01T22:38:02.513Z
Answers.LastActivityDate: 2017-07-01T22:38:02.513Z
Answers.UserDisplayName: auden
Answers.Body: <p>As far as I know, the word Function does not have a different meaning in mathematics and computers.  To compute is to do mathematics, and all a computer does is compute functions - nothing more.  Functions (and yes, that includes subroutines) in computers are functions in mathematics, period.  They may be functions of other functions, and so on, but they are nonetheless functions, and that is, by definition, all they are.</p>
  
  <p>If there has been some confusion among your students in your teaching of the word Function, can you consider the possibility that your students are confused because you are teaching them the exact opposite of what is in fact true?</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-27T01:12:47.630Z
Answers.Text: Hi Johnny, welcome to [cseducators.se]!  We are quite glad to see you here!  However, this answer is incorrect.  A mathematical function $f(x) = ...$ must always return the same value given the same $x$, but that is not the case for a programming function.  (See OP's `rand()` as a perfect example.)  In addition, a mathematical function must always return a result, while a programming function can instead produce a side-effect.  (See [mutators](https://en.wikipedia.org/wiki/Mutator_method).)
Answers.UserDisplayName: Ben I.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-27T14:14:53.587Z
Answers.Text: @johnny, what "function in mathematics" is something like `function foo() {  echo "Hello"; }` ?  (going javascript-ic). Nowadays, in most programming languages, the word *function* refers to a piece of code with parameters which does some actions and maybe transmits a value when ended.
Answers.UserDisplayName: Michel Billaud
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-02T14:14:46.850Z
Answers.Text: Computers do only one thing at the atomic level, and that is process functions.  That's why they are called computers.  Everything in a computer is either a function or an argument, and everything is deduced down to a function.  A function of functions is still a function.  This is BASIC stuff (no pun intended).  If you don't understand that, you cannot teach CS.  You can teach programming, I suppose, but certainly not science.  I cannot be polite or diplomatic here.  Many of you have literally no idea what you are talking about, and you are imbuing deeply flawed concepts into your students.
Answers.UserDisplayName: Johnny
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-02T14:20:02.537Z
Answers.Text: "must always return the same value given the same x, but that is not the case for a programming function."  Ben, there is NO SUCH THING as random.  This is accepted science in the CS community.  It may be a question in the physics community, but until then, it is MOST DEFINITELY not a question in the computer science community, except maybe in some deep theoretical circles.
Answers.UserDisplayName: Johnny
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-02T21:48:03.593Z
Answers.Text: @johnny You are again incorrect. There are plenty of (computer) functions that return truly random numbers that take advantage of water eddies and even a provably random function that had been created using quantum randomness.
Answers.UserDisplayName: Ben I.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-02T21:50:14.883Z
Answers.Text: @Johnny Even if we are to accept your definitions, no one would be coding mathematical functions in C because the metaphors of C (and even assembly) don't involve thinking at the level of mathematical functions. The "functions" you are referring to only exist at the level of electronics and booleans. If you believe that CS1 should begin at carry-adder circuits, feel free to develop a course. Typically, circuits don't enter into CS until year 3.
Answers.UserDisplayName: Ben I.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-02T21:54:33.540Z
Answers.Text: @Johnny The reason no one seems to be agreeing with you is because you are saying a lot of (provably) incorrect things. You are welcome to come here and contribute and learn, but you MUST be polite and diplomatic. Consider this your warning: this is a forum for reasoned discourse.  Ideas can be attacked, but not people. "Be Nice" is a serious rule here, and you have been pushing its boundaries to the limit. If you cannot abide by these rules, you will be sanctioned.
Answers.UserDisplayName: Ben I.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-26T23:29:44.283Z
Answers.LastActivityDate: 2019-02-26T23:29:44.283Z
Answers.UserDisplayName: Johnny
Body: <p>The word <code>function</code> has different meanings in math and computer science. For example, <code>f(x) = rand(1)</code> is a valid (pseudocoded) CS function, but it is not a mathematical function because it has multiple possible return values for a given argument. </p>
  
  <p>When and how is it most effective to bring the students' attention to the contrast? In teaching physics, it is a general principle that when possible a student should experience a phenomenon before being given words to describe it. (E.g., have students feel a torque before defining "torque".) (Arons, A. 1997. Teaching introductory Physics. New York, NY: Wiley). Presumably, there is a benefit to be obtained by introducing math functions, CS functions, and the contrast in a particular order and using a particular approach to active learning, but I am unaware of any published result to that effect. What ideas and data can people contribute to this question?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-01T17:05:11.393Z
Comments.Text: Presumably you're not interested in discussing the the interpretation of functions in functional programming, where functions generally **don't** have side effects and act like functions in mathematics? Or would this be something you'd be interested in discussing in an answer?
Comments.UserDisplayName: Aurora0001
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-01T17:08:24.713Z
Comments.Text: @Aurora0001, I think the example I give demonstrates a difference between math and CS functions even in a functional paradigm. My question is about teaching programming to novices, some of whom do learn CS in a primarily functional paradigm, especially CS newcomers in an undergraduate Matlab-based course.
Comments.UserDisplayName: Bennett Brown
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-01T18:02:58.270Z
Comments.Text: The floor function f has f(3.1)=f(3.2)=3, but each argument has one result. In contrast, the random number generator rand(3.1) is sometimes 0.1 and other time 0.2. It is not a mathematical function. There are other differences. Math functions specify a mapping without an inherent procedure, so you cannot talk about the time complexity of a math function. A CS function is a procedure to be executed without necessarily producing a mapping because some arguments may result in an infinite loop.
Comments.UserDisplayName: Bennett Brown
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-01T18:22:51.190Z
Comments.Text: my apologies, I read `round` instead of `rand`.
Comments.UserDisplayName: ItamarG3
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-02T16:52:53.903Z
Comments.Text: "but it is not a mathematical function because it has multiple possible return values for a given argument." Has it? if you take the whole state of the computer and recreate it exactly the same way, it would again give the exact same result. The state of the computer is an implicit input to this function, one that is often forgotten.
Comments.UserDisplayName: Polygnome
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-17T20:17:54.243Z
Comments.Text: I never really even think about the difference.  I look at it like this:  f(x) = 2x could be called a mathematical function.  But the function as it exists purely has nothing to do with what I just wrote.  A computer program like (lambda (x) (* 2 x)) actually embodies something of that purity.  But (lambda (x) (* 2 x)) is no more a function than "f(x) = 2x" is.  They are both notations, abstractions with shortcomings as well as applications to the genuine mathematical concept.
Comments.UserDisplayName: Darren
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-17T12:28:59.233Z
Comments.Text: @Polygnome Adding the state of the computer adds another level of indirection. Adding a level of indirection solves all engineering problems, but it has a huge cognitive impact.
Comments.UserDisplayName: Gilles 'SO- stop being evil'
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-17T23:18:41.903Z
Comments.Text: @Gilles This indirection is there whether you like it or not. Functional languages like Haskell only formalize it. And in Haskell there is no rand function like in imperative languages. If you want a random number, you really need to feed the world state (or PRNG state) into the function, and you get back the new state. Formal semantics are just the same as of mathematical function.
Comments.UserDisplayName: Frax
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-06-01T01:30:15.643Z
Comments.Text: Finally realizing that "f of x" *was* a function - just like I was used to in programming - got me through algebra.  This is what happens when you learn to program long before you get past long division using paper and pencil. I've come to think that math is a rather poor programming language with nondescriptive variables, no logic, confusing syntax for loops, and no `man` pages. :)
Comments.UserDisplayName: ivanivan
Tags: <best-practice><mathematics>
Title: Teaching "math function" vs. "CS function"
ThreadText: # Teaching "math function" vs. "CS function"
  
  (Asked by: username_0 on 2017-07-01)
  
  The word `function` has different meanings in math and computer science. For example, `f(x) = rand(1)` is a valid (pseudocoded) CS function, but it is not a mathematical function because it has multiple possible return values for a given argument.
  
  When and how is it most effective to bring the students' attention to the contrast? In teaching physics, it is a general principle that when possible a student should experience a phenomenon before being given words to describe it. (E.g., have students feel a torque before defining "torque".) (Arons, A. 1997. Teaching introductory Physics. New York, NY: Wiley). Presumably, there is a benefit to be obtained by introducing math functions, CS functions, and the contrast in a particular order and using a particular approach to active learning, but I am unaware of any published result to that effect. What ideas and data can people contribute to this question?
  
  **Tags:** best-practice, mathematics
  
  > **Comments:**
  > **username_1**: Presumably you're not interested in discussing the the interpretation of functions in functional programming, where functions generally **don't** have side effects and act like functions in mathematics? Or would this be something you'd be interested in discussing in an answer?  
  > **username_0**: @username_1, I think the example I give demonstrates a difference between math and CS functions even in a functional paradigm. My question is about teaching programming to novices, some of whom do learn CS in a primarily functional paradigm, especially CS newcomers in an undergraduate Matlab-based course.  
  > **username_0**: The floor function f has f(3.1)=f(3.2)=3, but each argument has one result. In contrast, the random number generator rand(3.1) is sometimes 0.1 and other time 0.2. It is not a mathematical function. There are other differences. Math functions specify a mapping without an inherent procedure, so you cannot talk about the time complexity of a math function. A CS function is a procedure to be executed without necessarily producing a mapping because some arguments may result in an infinite loop.  
  > **username_2**: my apologies, I read `round` instead of `rand`.  
  > **username_3**: "but it is not a mathematical function because it has multiple possible return values for a given argument." Has it? if you take the whole state of the computer and recreate it exactly the same way, it would again give the exact same result. The state of the computer is an implicit input to this function, one that is often forgotten.  
  > **username_4**: I never really even think about the difference.  I look at it like this:  f(x) = 2x could be called a mathematical function.  But the function as it exists purely has nothing to do with what I just wrote.  A computer program like (lambda (x) (* 2 x)) actually embodies something of that purity.  But (lambda (x) (* 2 x)) is no more a function than "f(x) = 2x" is.  They are both notations, abstractions with shortcomings as well as applications to the genuine mathematical concept.  
  > **username_5**: @username_3 Adding the state of the computer adds another level of indirection. Adding a level of indirection solves all engineering problems, but it has a huge cognitive impact.  
  > **username_6**: @username_5 This indirection is there whether you like it or not. Functional languages like Haskell only formalize it. And in Haskell there is no rand function like in imperative languages. If you want a random number, you really need to feed the world state (or PRNG state) into the function, and you get back the new state. Formal semantics are just the same as of mathematical function.  
  > **username_7**: Finally realizing that "f of x" *was* a function - just like I was used to in programming - got me through algebra.  This is what happens when you learn to program long before you get past long division using paper and pencil. I've come to think that math is a rather poor programming language with nondescriptive variables, no logic, confusing syntax for loops, and no `man` pages. :)  
  
  ---
  
  ## 5 answers
  
  ### Answer by username_8 on 2018-05-16. Score: 3
  
  Your *specific* question is whether there have been any studies of whether it is more effective to:
  
  * Introduce mathematical functions first, then explain how computer program functions can be nondeterministic or have side effects, or
  * Introduce computer programming functions first, then explain how mathematical functions are a disciplined subset of those that have a fixed deterministic mapping.
  
  I am not aware of any research on this question. My intuition is that it would be difficult to run such an experiment, because students encounter the mathematical notion of function when they are twelve years old. **I've taught since the 80's and on two continents, and have never seen a beginning programming student who hadn't already seen mathematical functions first**.
  
  Your *implied* question is how best to teach the difference. In my experience, beginning programmers quickly learn that we redefine (or misuse) lots of words from other fields, and don't get hung up on vocabulary.
  
  Again in my experience, the distinction that seems to help is differentiating between *nondeterminism* and *side effects*. Your example of `rand` falls in the former case: it looks like a function, it has a domain and a range, but it doesn't always give the same answer. That concept is pretty specific to CS and is (as they say) a good pedagogical opportunity.
  
  When the concept of side effect is introduced it is usually best to be explicit about which object is maintaining the state, and call the "function" a *method*.
  
  > **Comments:**
  > **username_5**: What levels did you teach? I wrote my first functions in Logo in primary school, and I didn't learn mathematical functions until high school. Surely I can't be the only one. And students _do_ have a difficulty with math functions that doesn't apply to computer functions, namely, the fact that in math, different algorithms that compute the same result for every input are the same function.  
  > **username_9**: Perhaps you didn't see functions formally, but you were intimately acquainted with the fact that `2 + 2` is always `4`.  
  
  ---
  
  ### Answer by username_10 on 2018-05-17. Score: 3
  
  A bit of history may help: the (now confusing) word `FUNCTION` was introduced in FORTRAN II (1958, see manual [here](http://archive.computerhistory.org/resources/text/Fortran/102653989.05.01.acc.pdf)), together with SUBROUTINE as facilities in order to "effectively enable the programmer to expand the language of the system indefinitely".
  
  In the original version of FORTRAN, the programmer simply used formulaes, which were "a kind of arithmetic statements" (P10), under the form of a function name followed by arguments enclosed in parentheses etc.
  
  ```
  FIRSTF(X) = A*X + B
  
  ```
  
  and were clearly intended (at least, if you the formula doesn't refer to functions with side-effects) to be side-effect free.
  
  The new FUNCTION device was here to allow more complex functions with several statements and auxiliary variables. Also for separate compilation and code reuse. Opened the door to use FUNCTION for code units that do some processing, possibly with side-effects, and incidentally return some value. Say, an error code ?
  
  The distinction between FUNCTION and SUBROUTINE was later blurred with C. Everybody is a so-called function now. Not necessarily related to the mathematical notion of functions.
  
  In my current way of teaching to novice programmers, I first introduce "void functions/methods" without parameters as groups of actions. The use of the Processing environment helps here as it is easy to give the example of a big drawing with parts defined in methods for obvious convenience.
  
  
  ---
  
  ### Answer by username_11 on 2017-07-01. Score: 3
  
  In mathematics a function is a constrained relation between two sets. You can illustrate that in a number of ways. The constraint implies consistence of operation and uniqueness of result.
  
  If you discuss relations (in math) as well as functions it may be easier to get beginning students to grok it.
  
  In most computing languages, a function is an operation that produces a value (and possibly side effects - horror). There is nothing in the definition of a Java function about constraint, not even that successive invocations with the same input produce the same values. A java function is a machine. Drop in 0 or more inputs, turn the crank, get some outputs. You can illustrate that by drawing a machine with an input hopper, and output spigot and a crank.
  
  Students who know math have a lot of problems early on with things that look like math but are not. Other questions in the "hopper" currently explore other aspects (equality, variables)
  
  > **Comments:**
  > **username_2**: uniqueness of result is not implied. In fact, just plot $x = 16y^2$ and you'll see multi-valued results.  
  > **username_11**: I was writing about functions. Your example is an equation (a relation). $f(y) = 16y^2$ is single valued. But yes, the equation has multiple solutions. That isn't the same thing. And of course, the unique result (range value) of a function could be a set. But still unique. Am I missing something? (deleted and re-entered, since I can't edit my earlier error.)  
  > **username_11**: @nocomprende, I think that is a good thing. In fact, I think that early CS education puts far too much stress on mathematical examples. That seems to be a consequence of trying to teach the primitives (int, float, ...) earlier than they need to be taught. There are exceptions, of course.  
  
  ---
  
  ### Answer by username_12 on 2017-07-01. Score: 2
  
  A function in mathematics is a "mapping" between one set and another set (in the non-strict definition, I think it suffices for this discussion). For example, a function $f: X \rightarrow Y$ where $X = \{a, b\}$ and $Y = \{1, 2\}$ could very well just be that $a = 1$ and $b = 2$. The key thing is you take one value $x\in X$ and you get *one* value $y\in Y$ (there are other constraints, but I have no wish to double check them or go into an in-depth mathematical discussion here).
  
  A function in computer science (at least the ones I'm used to, in for example Python) are just a set of commands that can take some inputs, produce some outputs, and do some things along the way. For example, the Python equivalent of $f$ above would be
  
  ```
  def someFunction(x):
      if x == 'a':
          return 1
      elif x == 'b':
          return 2
      else:
          return 'Stop messing with this pseudocode example, will ya?'
  
  ```
  
  But another function could be
  
  ```
  def anotherFunction(x, y, z):
      localVar = x+y
      list = []
      list.append(localVar)
      print("this function does all the things")
      return sum(list) + z
  
  ```
  
  The above, while a pretty useless function (I'm terrible at thinking up functions with no purpose =P), does a lot more things than a function in math does. It does some things on the side, returns some stuff, takes multiple inputs and outputs, and so on. It's a complicated beast. Heck, you can even write a function like
  
  ```
  def yetAnotherFunction():
      print('yay')
  
  ```
  
  That doesn't take any inputs or return anything.
  
  I guess, what I'm saying is - I think the two are so different, I'm not really sure there's a ton to be gained by exploring the contrast. A function in computer science is a module of code that can be called, perhaps with inputs, and does some stuff, maybe returning something. A function in math is a very strictly defined thing: you put something in, and you get only one thing out.
  
  > **Comments:**
  > **username_5**: What you're calling a “function in computer science”, I would call a “function in programming”. Functions in computer _science_ can get a lot more subtle, but that's something you'd teach only in a semantics course, not in a programming course.  
  > **username_5**: While you may start by saying that the word “function” means unrelated things in math and programming, that's only the start. There are both mathematical and cognitive associations between the two concepts. It would depend on the level and the interests of the students, but I think the relationship does need to be discussed at some point.  
  > **username_9**: Don't forget the (not at all) subtle difference in what "variable" means, either...  
  
  ---
  
  ### Answer by username_13 on 2019-02-26. Score: -3
  
  As far as I know, the word Function does not have a different meaning in mathematics and computers. To compute is to do mathematics, and all a computer does is compute functions - nothing more. Functions (and yes, that includes subroutines) in computers are functions in mathematics, period. They may be functions of other functions, and so on, but they are nonetheless functions, and that is, by definition, all they are.
  
  If there has been some confusion among your students in your teaching of the word Function, can you consider the possibility that your students are confused because you are teaching them the exact opposite of what is in fact true?
  
  > **Comments:**
  > **username_14**: Hi username_13, welcome to [cseducators.se]!  We are quite glad to see you here!  However, this answer is incorrect.  A mathematical function $f(x) = ...$ must always return the same value given the same $x$, but that is not the case for a programming function.  (See OP's `rand()` as a perfect example.)  In addition, a mathematical function must always return a result, while a programming function can instead produce a side-effect.  (See [mutators](https://en.wikipedia.org/wiki/Mutator_method).)  
  > **username_10**: @username_13, what "function in mathematics" is something like `function foo() {  echo "Hello"; }` ?  (going javascript-ic). Nowadays, in most programming languages, the word *function* refers to a piece of code with parameters which does some actions and maybe transmits a value when ended.  
  > **username_13**: Computers do only one thing at the atomic level, and that is process functions.  That's why they are called computers.  Everything in a computer is either a function or an argument, and everything is deduced down to a function.  A function of functions is still a function.  This is BASIC stuff (no pun intended).  If you don't understand that, you cannot teach CS.  You can teach programming, I suppose, but certainly not science.  I cannot be polite or diplomatic here.  Many of you have literally no idea what you are talking about, and you are imbuing deeply flawed concepts into your students.  
  > **username_13**: "must always return the same value given the same x, but that is not the case for a programming function."  Ben, there is NO SUCH THING as random.  This is accepted science in the CS community.  It may be a question in the physics community, but until then, it is MOST DEFINITELY not a question in the computer science community, except maybe in some deep theoretical circles.  
  > **username_14**: @username_13 You are again incorrect. There are plenty of (computer) functions that return truly random numbers that take advantage of water eddies and even a provably random function that had been created using quantum randomness.  
  > **username_14**: @username_13 Even if we are to accept your definitions, no one would be coding mathematical functions in C because the metaphors of C (and even assembly) don't involve thinking at the level of mathematical functions. The "functions" you are referring to only exist at the level of electronics and booleans. If you believe that CS1 should begin at carry-adder circuits, feel free to develop a course. Typically, circuits don't enter into CS until year 3.  
  > **username_14**: @username_13 The reason no one seems to be agreeing with you is because you are saying a lot of (provably) incorrect things. You are welcome to come here and contribute and learn, but you MUST be polite and diplomatic. Consider this your warning: this is a forum for reasoned discourse.  Ideas can be attacked, but not people. "Be Nice" is a serious rule here, and you have been pushing its boundaries to the limit. If you cannot abide by these rules, you will be sanctioned.  
  
  ---
  
  