What is the difference between a subroutine and a function?

CodeBlue picture CodeBlue · Apr 30, 2012 · Viewed 12.3k times · Source

Possible Duplicate:
What is the difference between a ‘function’ and a ‘procedure’?

I searched online for an answer to this question, and the answer I got was that a function can return a value, modify a value, etc., but a subroutine cannot. But I am not satisfied with this explanation and it seems to me that the difference ought to be more than just a matter of terminology.

So I am looking for a more conceptual answer to the question.

Answer

Oliver Charlesworth picture Oliver Charlesworth · Apr 30, 2012

A function mirrors the mathematical definition of a function, which is a mapping from 1 or more inputs to a value.1

A subroutine is a general-purpose term for any chunk of code that has a definite entry point and exit point.

However, the precise meaning of these terms will vary from context to context.


1. Obviously, this is not the formal mathematical definition of a function.