Function (programming): Difference between revisions
In C and other languages, the term "function" is used in a wider sense than this article previously allowed. |
No edit summary |
||
Line 1: | Line 1: | ||
In computer programming, a '''function''' is a [[subroutine]] which can be called with a set of [[parameter]]s and which is defined or declared as though it always returns a value, sometimes using a special symbol to indicate that no value is ever actually returned. For example, in the [[C programming language|C]] and [[Java programming language]]s, a function that never returns a value would be defined as though the returned value was of type ''void''. |
In computer programming, a '''function''' is a [[subroutine]] which can be called with a set of [[parameter]]s and which is defined or declared as though it always returns a value, sometimes using a special symbol to indicate that no value is ever actually returned. For example, in the [[C programming language|C]] and [[Java programming language]]s, a function that never returns a value would be defined as though the returned value was of type ''void''. The only difference between a void function and a [[procedure]] is terminological. |
||
If a function can in fact return a value, then the function may be referenced within an [[expression]], in which case if a value is returned, it is used in the natural way in the evaluation of the expression. |
If a function can in fact return a value, then the function may be referenced within an [[expression]], in which case if a value is returned, it is used in the natural way in the evaluation of the expression. |
||
Line 10: | Line 10: | ||
and if f(1) returns 1, and if g(2) returns 2, then the expression ''f(1) + f(2)'' |
and if f(1) returns 1, and if g(2) returns 2, then the expression ''f(1) + f(2)'' |
||
would evaluate to 3. |
would evaluate to 3. |
||
In [[procedural programming language]]s, functions may have [[side-effects]], that is to say, they may have outside inputs and outputs. This makes them not equivilant to [[function (mathematics)|mathematical functions]]. |
Revision as of 09:27, 28 March 2004
In computer programming, a function is a subroutine which can be called with a set of parameters and which is defined or declared as though it always returns a value, sometimes using a special symbol to indicate that no value is ever actually returned. For example, in the C and Java programming languages, a function that never returns a value would be defined as though the returned value was of type void. The only difference between a void function and a procedure is terminological.
If a function can in fact return a value, then the function may be referenced within an expression, in which case if a value is returned, it is used in the natural way in the evaluation of the expression.
For example, in the C programming language, in which 'int' signifies a sufficiently small integer, if f and g are declared as follows:
int f(int a); int g(int a);
and if f(1) returns 1, and if g(2) returns 2, then the expression f(1) + f(2) would evaluate to 3.
In procedural programming languages, functions may have side-effects, that is to say, they may have outside inputs and outputs. This makes them not equivilant to mathematical functions.