What is the difference between an algorithm and a function?
Solution 1
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.
Solution 2
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.
Solution 3
An algorithm is a set of instructions.
In computer programming, a function is an implementation of an algorithm.
Solution 4
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.
Solution 5
An algorithm describes the general idea, whereas a function is an actual working implementation of that idea.
Tyler Gillies
Updated on March 14, 2020Comments
-
Tyler Gillies over 4 years
Are they the same thing?
-
DarthVader almost 14 yearsunfortunately 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.
-
Lay González over 11 yearsThe algorithm may be just pseudocode.
-
Avisra over 10 yearsThat's not necessarily true. "function" can mean a unit of code as implemented in a particular language. It can also mean an abstract, functional definition of computation, based on the model of the lambda calculus.
-
runeks about 9 yearsI would argue that a function is a mathematical concept, and not necessarily related to a computer program, although we also use something called functions in computer programming. As far as I know, the mathematical function -- as a concept -- was introduced long before computer programming with functions existed.
-
riwalk about 9 years@runeks, considering the fact that this is a website for programming related questions, I would consider that fact pretty irrelevant.
-
Daniel Asher almost 9 yearsthis is the better answer.
-
Daniel Asher almost 9 yearsa set is an ambiguous word here, though it probably can be clarified.
-
Jenia Ivanov over 8 yearsit's the other way around lol
-
RFV over 4 yearsIn computer programming, a function can be an implementation of an algorithm, it can also simply update state, or even only do input or output.