Making The Connections
Functions are isolated, and whats more nothing survives after they have finished. Put like this a function doesn't seem to be that useful because you can't get data values in, you can't get data values out, and they don't remember anything that happens to them!
To be useful there has to be a way of getting data into and out of a function, and this is the role of the curved brackets. You can define special variables called parameters which are used to carry data values into a function. Parameters are listed and declared in between the () brackets in the function's definition. For example:
sum( int a, int b)
result=a + b;
defines a function called sum with two parameters a and b, both integers. Notice that the result variable is declared in the usual way within the body of the function. Also, notice that the parameters a and b are used within the function in the same way as normal variables - which indeed they are. What is more, they are still local variables and have nothing at all to do with any variables called a and b defined in any other function.
The only way in which parameters are any different is that you can give them initial values when the function starts by writing the values between the round brackets. So
is a call to the sum function with a set to 1 and b set to 2 and so result is set to 3. You can also initialise parameters to the result of expressions such as:
which will set a equal to whatever x+2 works out to be and b equal to whatever z*10 works out to be.
As a simpler case you can also set a parameter to the value in a single variable - for example:
will set a to the value stored in x and b to the value stored in y.
Parameters are the main way of getting values into a function, but how do we get values out? There is no point in expecting the &&&&result variable to somehow magically get its value out of the sum function - after all, it is a local variable and is destroyed when sum is finished. You might try something like:
sum(int a, int b, int result)
result = a + b;
but it doesn't work. Parameters are just ordinary variables that are set to an initial value when the function starts running - they don't pass values back to the program that used the function. That is:
doesn't store 1+2 in r because the value in r is used to initialise the value in result and not vice versa. You can even try
and it still will not work - the variable result within the function has nothing to do with the variable result used in any other program.
The simplest way to get a value out of a function is to use the return instruction. A function can return a value via its name - it's as if the name was a variable and had a value. The value that is returned is specified by the instruction:
which can occur anywhere within the function, not just as the last instruction - however, a return always terminates the function and returns control back to the calling function. The only complication is that as the function's name is used to return the value it has to be given a data type. This is achieved by writing the data type in front of the function's name. For example:
So now we can at last write the correct version of the sum function:
int sum(int a, int b)
result = a + b;
and to use it you would write something like:
which would add 1 to 2 and store the result in r. You can use a function anywhere that you can use a variable. For example,
is perfectly OK, as is
Obviously, the situation with respect to the number of inputs and outputs of a function isn't equal. That is you can create as many parameters as you like but a function can return only a single value. (Later on we will have to find ways of allowing functions to return more than one value.)
So to summarise: a function has the general form:
type FunctionName(type declared parameter list)
statements that make up the function
and of course a function can contain any number of return statements to specify its return value and bring the function to an end.
There are some special cases and defaults we need to look at before moving on. You don't have to specify a parameter list if you don't want to use any parameters - but you still need the empty brackets! You don't have to assign the function a type in which case it defaults to int. A function doesn't have to return a value and the program that makes use of a function doesn't have to save any value it does return. For example, it is perfectly OK to use:
which simply throws away the result of adding 1 to 2. As this sort of thing offends some programmers you can use the data type void to indicate that a function doesn't return a value. For example:
is a function with no parameters and no return value.
void is an ANSI C standard data type.
The break statement covered in a previous section can be used to exit a function. The break statement is usually linked with an if statement checking for a particular value. For example:
if (x==1) break;
If x contained 1 then the fuction would exit and return to the calling program.