What is the difference between a "function" and a "procedure"?

rpr picture rpr · Apr 6, 2009 · Viewed 330.3k times · Source

Generally speaking, we all hear about the functions or procedures in programming languages. However, I just found out that I use these terms almost interchangeably (which is probably very wrong).

So, my question is:

What is the difference in terms of their functionality, their purpose and use?

An example would be appreciated.

Answer

Toon Krijthe picture Toon Krijthe · Apr 6, 2009

A function returns a value and a procedure just executes commands.

The name function comes from math. It is used to calculate a value based on input.

A procedure is a set of command which can be executed in order.

In most programming languages, even functions can have a set of commands. Hence the difference is only in the returning a value part.

But if you like to keep a function clean, (just look at functional languages), you need to make sure a function does not have a side effect.