Given two sets X and Y, a function f: X --> Y is something that associates with every element of X an element of Y. A partial function is similar, but need not be defined for every element of X. Some textbooks call non-partial functions total functions. The set X is called f's domain; Y, its codomain or range.

A function can be considered to be a special kind of relation, namely, one in which no two pairs have the same first element.

Some mathematicians make a point of not defining functions in terms of set theoretic constructs; this happens, for instance, in category theory, where functions are the primitive notion, and sets + relations are defined in terms of them.

A function is surjective (or 'onto') if it covers all of its range; it is injective (or 'one to one') if it maps no two elements to the same value. A bijective function is both surjective and injective: it has an inverse function.

Also, of course, one name for the fundamental unit of modularity in a computer program, also called subroutine or procedure. Externally, a function is known by a name, accepts zero or more arguments or parameters describing the data it is to do its work on, and returns a value reporting the result. (The analogy with mathematical functions is clear.) Ideally, a function implements an abstraction, in a black box way: it performs some useful service, but you don't know (and don't care) how it does it. Theoretically, it should always be possible to rewrite the internal implementation of a function -- to fix a bug, or improve performance, or add a feature -- and, as long as the parameter list doesn't change, for the rewrite to be transparent to the function's callers, such that none of them have to make any corresponding changes themselves.

Examples (suggested by wharfinger):

A C function to return the average of two numbers:

int average(int a, int b)
{
return (a + b) / 2;
}

A Pascal function for computing the greatest common divisor (from Jensen & Wirth, Pascal: User Manual and Report, 2/E, p. 82):

function gcd(m,n: integer):integer;
begin if n=0 then gcd := m
else gcd := gcd(n,m mod n)
end;

In a computer program, a function is a unit of code with at least a name (there are actually exceptions to this, see below), and in most languages also a return value and argument list.

Some languages differentiate between functions and subroutines, where a subroutine is a unit with no return value, and a function is a unit with a return value.

You can conceptualise a function as a black box. You tell it to perform an action (specifying how it should perform this action or on what data it should perform it with parameters/arguments), and then catch the return value (if it gives one). This is the common Input -> Process -> Output flow.

How the function performs the process is, ideally, of no importance to the client using it. Here are two functions that perform the same thing in two different ways, with no discernable difference to the outside world (apart from perhaps runtime speed) -

int DivideIntByTwoAndReturn( int i ) {
  return i/2;
}

int DivideIntByTwoAndReturnAgain( int i ) {
  return (i*2)/4;
}
This is a strong reason to not put data members in the public interface of a class in OOP, but rather to access them through member functions. We can do all sorts of things in the access functions - range checking, access counter, functional abstraction1.

As mentioned above, there are such things as anonymous functions. Consider the following:

(X) => X / 10

That function has an argument list (its argument is X), and a return value (it applies the operation X / 10 to its argument), but it has no name! We would apply it to the value 20 like so -

((X) => X / 10)(20)

What good is this? Well, it's not that useful on scalar values, but if we want to apply the function to every element of a container (say, an associative array or simple vector), we could write something like this -

for ( int i = 0; i < 10; i++ ) {
  array[i] = ((X) => X / 10)(array[i] ));
}
Which will divide each element of array by 10 and put the result in place of the old element. (No, that's not legal C, but you get the idea).

In another example, LPC uses anonymous functions (actually pointers to anonymous functions) in some cases, as with the filter() function -

array = filter( array, (: $1->query_name("human") :) );

The above "calls" the anonymous function once for every element in the array, replacing $1 with the element of the array. filter() then returns a structure containing each element for which the function returned nonzero.

See lambda calculus for more information on anonymous functions.


1. Imagine a class X that keeps track of an average, A. It can either make A a public data member and let people access it directly, or allow it to be accessed through a member function GetA(). By choosing the second option it has the option of calculating the average each time, or just returning a pre-calculated average, without clients knowing. The implemenator can switch between the two without effecting clients.

Functions are your friends

A function, in Mathematics, will take a number as input and spit out an output somehow related to it. In other words, by giving a function input x, we are given y as output, having done something to x along the way in order to get this. A function can be classed as either many-to-one, in that there are a number of possible x that will give the same resultant y, or one-to-one, in that every input has its own output, and no two x can give the same y.

A function can be written as follows:

f : x --> 2x + 1   x

A function can also be written simply as f (x). A separate function of x could be written as g (x), and so on.

The above function ( f : x --> 2x + 1 ) means that for a value of x, this function will double it (2x) and then add 1 to this to give out output. So, if we were to take x = 3, we are given an output of 7. The part following this ( x ∈ ℜ ) is known as the domain, and defines which set of numbers can be used as inputs. Here, x can be any number which is a member of the set of real numbers.

A function will also have what is known as its range, which is simply the possible outputs for the function. Here, the outputs can be any real number, so the range is defined as f (x) ∈ ℜ.

If the differential of a function is always either positive or negative, then that function must be a one-to-one function. For example, imagine the function f : x |--> x 3. If we write it as y = x3, and differentiate, we are given 3x2, which will always be positive - so the function will be one-to-one.

Func"tion (?), n. [L. functio, fr. fungi to perform, execute, akin to Skr. bhuj to enjoy, have the use of: cf. F. fonction. Cf. Defunct.]

1.

The act of executing or performing any duty, office, or calling; performance. "In the function of his public calling." Swift.

2. (Physiol.)

The appropriate action of any special organ or part of an animal or vegetable organism; as, the function of the heart or the limbs; the function of leaves, sap, roots, etc.; life is the sum of the functions of the various organs and parts of the body.

3.

The natural or assigned action of any power or faculty, as of the soul, or of the intellect; the exertion of an energy of some determinate kind.

As the mind opens, and its functions spread.
Pope.

4.

The course of action which peculiarly pertains to any public officer in church or state; the activity appropriate to any business or profession.

Tradesmen . . . going about their functions.
Shak.

The malady which made him incapable of performing his
regal functions.
Macaulay.

5. (Math.)

A quantity so connected with another quantity, that if any alteration be made in the latter there will be a consequent alteration in the former. Each quantity is said to be a function of the other. Thus, the circumference of a circle is a function of the diameter. If x be a symbol to which different numerical values can be assigned, such expressions as x2, 3x, Log. x, and Sin. x, are all functions of x.

Algebraic function, a quantity whose connection with the variable is expressed by an equation that involves only the algebraic operations of addition, subtraction, multiplication, division, raising to a given power, and extracting a given root; -- opposed to transcendental function. --
Arbitrary function. See under Arbitrary. --
Calculus of functions. See under Calculus. --
Carnot's function (Thermo-dynamics), a relation between the amount of heat given off by a source of heat, and the work which can be done by it. It is approximately equal to the mechanical equivalent of the thermal unit divided by the number expressing the temperature in degrees of the air thermometer, reckoned from its zero of expansion. --
Circular functions. See Inverse trigonometrical functions (below). -- Continuous function, a quantity that has no interruption in the continuity of its real values, as the variable changes between any specified limits. --
Discontinuous function. See under Discontinuous. --
Elliptic functions, a large and important class of functions, so called because one of the forms expresses the relation of the arc of an ellipse to the straight lines connected therewith. --
Explicit function, a quantity directly expressed in terms of the independently varying quantity; thus, in the equations y = 6x2, y = 10 - x3, the quantity y is an explicit function of x. --
Implicit function, a quantity whose relation to the variable is expressed indirectly by an equation; thus, y in the equation x2 + y2 = 100 is an implicit function of x. --
Inverse trigonometrical functions, or Circular function, the lengths of arcs relative to the sines, tangents, etc. Thus, AB is the arc whose sine is BD, and (if the length of BD is x) is written sin -1x, and so of the other lines. See Trigonometrical function (below). Other transcendental functions are the exponential functions, the elliptic functions, the gamma functions, the theta functions, etc. --
One- valued function, a quantity that has one, and only one, value for each value of the variable. --
Transcendental functions, a quantity whose connection with the variable cannot be expressed by algebraic operations; thus, y in the equation y = 10x is a transcendental function of x. See Algebraic function (above). --
Trigonometrical function, a quantity whose relation to the variable is the same as that of a certain straight line drawn in a circle whose radius is unity, to the length of a corresponding are of the circle. Let AB be an arc in a circle, whose radius OA is unity let AC be a quadrant, and let OC, DB, and AF be drawnpependicular to OA, and EB and CG parallel to OA, and let OB be produced to G and F. E Then BD is the sine of the arc AB; OD or EB is the cosine, AF is the tangent, CG is the cotangent, OF is the secant OG is the cosecant, AD is the versed sine, and CE is the coversed sine of the are AB. If the length of AB be represented by x (OA being unity) then the lengths of Functions. these lines (OA being unity) are the trigonometrical functions of x, and are written sin x, cos x, tan x (or tang x), cot x, sec x, cosec x, versin x, coversin x. These quantities are also considered as functions of the angle BOA.

 

© Webster 1913


Func"tion (?), Func"tion*ate (?), v. i.

To execute or perform a function; to transact one's regular or appointed business.

 

© Webster 1913


Func"tion (?), n.

1. (Eccl.)

A religious ceremony, esp. one particularly impressive and elaborate.

Every solemn ‘function' performed with the requirements of the liturgy.
Card. Wiseman.

2.

A public or social ceremony or gathering; a festivity or entertainment, esp. one somewhat formal.

This function, which is our chief social event.
W. D. Howells.

 

© Webster 1913

Log in or register to write something here or to contact authors.