193

7
+5  Q:

## What is the difference between an algorithm and a function?

Are they the same thing?

+18  A:

No.

A function is a block of code in a computer program.

An algorithm is an abstract concept that describes how to solve a problem.

unfortunately there is no formal and precise definition of algorithm. By Church-Turing thesis, there is an universally accepted definition which is informal. Even CLRS start with saying "Informally an algorithm is...". Even this answer states "abstract concept" which is quite ambiguous.
+2  A:

An algorithm is a set of instructions.

In computer programming, a function is an implementation of an algorithm.

+1  A:

An algorithm describes the general idea, whereas a function is an actual working implementation of that idea.

A:

It might be almost a philosophical question, but I'de say an algorithm is the answer(or how-to) to a problem at hand where as a function does not necerally answer one problem in itself.

What you want to do normally is split your algorithm into severals function that each has their own goal, which, in the end, will achieve the problem at hand, when used together.

Ex : You want to Sort a list of numbers. The algorightm used would be for example the Merge-sort algorithm. That specific algorithm is actually composed of more than one functions, one that will split your array, another to check for equality, another to merge everything back together, and so on.

+6  A:

In mathematics, a function is "a mathematical relation such that each element of a given set (the domain of the function) is associated with an element of another set (the range of the function)" (source - google.com, define:function).

In computer science, a function is a piece of code that optionally takes parameters, optionally gives a result, and optionally has a side effect (depending on the language - some languages forbid side-effects). It must have a specific machine implementation in order to execute.

The computer science term came out of the mathematical term, being the machine implementation of the mathematical concept.

An algorithm is "a precise rule (or set of rules) specifying how to solve some problem" (source - google.com, define:algorithm). An algorithm can be defined outside of computer science, and does not have a definitive machine implementation. You can "implement" it by writing it out by hand :)

The key difference here is, in computer science, an algorithm is abstract, and doesn't have a definitive machine implementation. A function is concrete, and does have a machine implementation.

A:

An Algorithm usually refers to the method or process used to end up with the result after mathematical processing. A Function is a subroutine used to avoid writing the same code over and over again. They are different in their uses. For instance, there may be an Algorithm that is used for encrypting data, and a function for posting code to a webpage.

Here is some further reference:

http://en.wikipedia.org/wiki/Algorithm

+1  A:

An algorithm is a series of steps (a process) for performing a calculation, whereas a function is the mathematical relationship between parameters and results.

A function in programming is different than the typical, mathematical meaning of function because it's a set of instructions implementing an algorithm for calculating a function.