# Functions

Functions are used to divide a program up into smaller more manageable components. The basic structure is the same as the main program (which is itself just a function called `main`). The general form is:

```<return_type> procname(<parameter_type_1> p1, <parameter_type_2> p2 ...)
{
<type_1>    ...;
<type_2>    ...;

s1;
s2;
...
return <value of type return_type>;
}
```

Note that the body of the function is a block, as described in section 9, and that variables declared within the function behave as described in section 9.

Example: definition of a factorial function, and a function to calculate nCr:

```// Factorial function declaration (prototype)
int factorial(int n);

// Combinations function definition
int combinations(int n, int r)
{
return factorial(n) / (factorial(n - r)*factorial(r));
}

// Factorial function definition
int factorial(int n)
{

// Calculate the factorial with a FOR loop
for (int i = 1; i <= n; ++i)
{
}

}
```

The value (of type `return_type`) returned by a function can be used as a component of an expression. Thus given the above functions and the variables `total`, `count` and `comb` also of type `int`, the following expression and assignment may be used compute totalCcount and store the result in `comb`:

```comb = combinations(total, count);
```

The formal parameters of a procedure (e.g. `int n` and `int r` in `combinations`) specify the name and type for each argument. A formal parameter preceded by `&` indicates call by reference. The corresponding actual parameter must be a variable of the same type, and statements within the function can modify its value.

If the `&` is omitted then call by value is indicated. The actual parameter can be any expression yielding a value of the required type, and a copy is assigned to the corresponding formal parameter, i.e. it is an input-only parameter. The parameters for `factorial` and `combinations` are all passed by value.

Declaring the function to be of type `void` indicates that no value is returned by the call. The following functions illustrate `void` and call by reference:

```void swap(float &a, float &b)
// Swaps x and y data of calling function.
{
float tmp;
tmp = b;
b = a;
a = tmp;
}

void sort(float data[], int length)
{
int      minj;
float    min;

for (int i = 0; i < length-1; i++)
{
minj = i;
min = data[i];
for (int j = i+1; j < length; j++)
{
if (data[j] < min)
{
minj = j;
min = data[j];
}
}
if (minj > i)
swap(data[minj], data[i]);
}
}
```

Arrays are always passed by reference, and the function can access and/or modify all the elements of the array. This avoids the expense of making a copy of each element of the array.

Functions may call themselves (and others) recursively, as illustrated in the following version of the `factorial` function:

```int r_factorial(int a)
{
int result;

if (a <= 1)
result = 1;
else
result = a * r_factorial(a - 1);   // recursive call

return result;
}
```
• © Cambridge University, Engineering Department, Trumpington Street, Cambridge CB2 1PZ, UK (map)
Tel: +44 1223 332600, Fax: +44 1223 332662