What is the Difference Between Functions and Procedures?

[Note: Hadi Partovi and Baker Franke of Code.org contributed a lot of this and Emmanuel Schanzer offered ideas.  I don’t really know how to assign credit to specific pieces, but I want to make sure to acknowledge their efforts.  Editorial comments about possible implications of the vocabulary issues are mine.  — JEP]

In mathematics, the word “function” means this:

For a consistent set of inputs, a function returns a single, consistent output.

Notationally, a function is typically a single letter such as f, g, or h, followed by parentheses containing parameters such as x, y, and z.  The parameters are generally referred to as variables in a high school math class.

For example, if f(x) = 2x + 3, then f(5) = 13.  Always.

If g(x, y) = x * y, then g(4, 5) = 20.  Always.

If h() = 5, then h() = 5.  Always.

Mathematical functions need not take any inputs.  But they do need to be consistent.  Students are taught the vertical line test for graphs where y = f(x) to verify that consistency of output.

In computer science, programming languages allow people to give functions more descriptive names.  For example, if you want to write a function that takes a number as an input and returns the square of that number, you could name that function square.  In the programming language JavaScript, that would look like:

function square(x) {
  return x * x;
}

(Isn’t that much nicer than f(x) = x2?  Isn’t it nice to see the name “square” and not think that it means to multiply s*q*u*a*r*e?)

In computer science, we have procedures.  A procedure is like a function in that it takes any number of inputs.  It is different in that it need not produce consistent outputs.

All functions are procedures.  Not all procedures are functions.

Here is a JavaScript procedure that is not a mathematical function:

function notReallyaFunction(x) {
  return random();
}

notReallyaFunction returns a random number between 0 and 1, regardless of x.  That’s not a problem for procedures in programming languages.  There is no requirement to return consistent outputs.

Note that the word function does not imply that a function is being created in the mathematical sense. It is a keyword in the JavaScript language used to create a procedure.

From the teacher’s point of view, this seems like a simple vocabulary fix, and it would likely be just that if math teachers could reliably be aware that the word function is being used in a non-mathematical way by programmers and if computer science teachers had the depth and breadth of knowledge to be aware that this should be discussed at least briefly with their students.  To their credit, Code.org does address this in their curriculum, even if it is an issue in this widely viewed video.

Vocabulary frustration can be a reason for students to tune out of programming. The AP Computer Science curriculum demands knowledge of the Java programming language. Explaining public static void main to the intellectually curious student is quite a challenge for teachers and problematic for some.  I’ve seen students who were very capable mathematicians tune out on day one of class because this vocabulary is intimidating.  I’ve had a parent, who works at Google, express frustration about expecting novices to deal with the overhead of getting started with the simplest program in Java.  It can be intimidating.  Minimizing and eliminating unnecessary duplication and conflation of terms can make a difference between a child participating and a child opting out of learning CS. (It might also help the child learning mathematics after programming to be less confused.)

The JavaScript programming language isn’t going to be changed. It is a widely used tool for creating all kinds of wonderful programs. It’s too bad that the designers chose the word function as a keyword, but that ship has sailed.

Still, my objection to the Code.org video remains. The topic is good, Chris Bosh is a tremendous messenger, and I still wish they would go back and change the word “FUNCTIONS” to “PROCEDURES”.

Leave a Reply

Your email address will not be published. Required fields are marked *