data-ad-format="horizontal">

# Functions as used in programming languages

Functions are used extensively in computer languages and spreadsheets. Recall that a function takes an input , does some calculations on the input, and then gives back a result. In computer programming they are a very similar idea, with a few changes to naming and properties.

A function in a programming language is a program fragment that 'knows' how to perform a defined task. For example a function may be written that finds the average of three supplied numbers. Once written, this function may be used many times without having to rewrite it over and over.

## Example - the function avg

```function avg(a,b,c)
{ var result = (a+b+c)/3;
return result;
}
```

The above, written in Javascript, performs the average function. On the first line, the name of the function is 'avg', It expects three inputs called a,b and c. In computer programming these are called parameters; they stand for the three values sent when the function is used. The function has its own private variable called result which is calculated from the parameters and then the function 'returns' the result;

## Using the function

In computer programming the act of using the function is "calling the function". In the program below there are two "calls" to the function. In each case, three particular values are sent as parameters and the result will be the average of the three.

```/* main program*/
..
var averageHt = avg(6, 4, 7);
..
..
..
var averageAge = avg(30, 45, 21);
..
```

So as you can see, functions in computer programming and spreadsheets are very similar to those in math, and serve to 'package' some calculations so it can be separated and used over and over.