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.