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?

  • 3
    $\begingroup$ 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? $\endgroup$ – Aurora0001 Jul 1 '17 at 17:05
  • 1
    $\begingroup$ @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. $\endgroup$ – Bennett Brown Jul 1 '17 at 17:08
  • 2
    $\begingroup$ "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. $\endgroup$ – Polygnome Jul 2 '17 at 16:52
  • 1
    $\begingroup$ @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. $\endgroup$ – Frax May 17 '18 at 23:18
  • 2
    $\begingroup$ 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. :) $\endgroup$ – ivanivan Jun 1 '18 at 1:30

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)

| improve this answer | |
  • 1
    $\begingroup$ uniqueness of result is not implied. In fact, just plot $x = 16y^2$ and you'll see multi-valued results. $\endgroup$ – ItamarG3 Jul 1 '17 at 18:24
  • 1
    $\begingroup$ 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.) $\endgroup$ – Buffy Jul 1 '17 at 20:00
  • 2
    $\begingroup$ @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. $\endgroup$ – Buffy Jul 2 '17 at 9:21

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.

| improve this answer | |
  • $\begingroup$ 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. $\endgroup$ – Gilles 'SO- stop being evil' May 17 '18 at 12:17
  • $\begingroup$ Perhaps you didn't see functions formally, but you were intimately acquainted with the fact that 2 + 2 is always 4. $\endgroup$ – vonbrand May 17 '18 at 19:45

A bit of history may help: the (now confusing) word FUNCTION was introduced in FORTRAN II (1958, see manual here), 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.


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.

| improve this answer | |

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
        return 'Stop messing with this pseudocode example, will ya?'

But another function could be

def anotherFunction(x, y, z):
    localVar = x+y
    list = []
    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():

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.

| improve this answer | |
  • 1
    $\begingroup$ 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. $\endgroup$ – Gilles 'SO- stop being evil' May 17 '18 at 12:20
  • $\begingroup$ 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. $\endgroup$ – Gilles 'SO- stop being evil' May 17 '18 at 12:25
  • $\begingroup$ Don't forget the (not at all) subtle difference in what "variable" means, either... $\endgroup$ – vonbrand May 17 '18 at 19:43

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?

| improve this answer | |
  • $\begingroup$ Hi Johnny, welcome to Computer Science Educators! 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.) $\endgroup$ – Ben I. Feb 27 '19 at 1:12
  • $\begingroup$ @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. $\endgroup$ – Michel Billaud Feb 27 '19 at 14:14
  • $\begingroup$ 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. $\endgroup$ – Johnny Mar 2 '19 at 14:14
  • $\begingroup$ "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. $\endgroup$ – Johnny Mar 2 '19 at 14:20
  • $\begingroup$ @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. $\endgroup$ – Ben I. Mar 2 '19 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.