Functions and Prototypes I

Functions - C's Building Blocks

A function is simply a chunk of C code (statements) that you have grouped together and given a name. The value of doing this is that you can use that "chunk" of code repeatedly simply by writing its name. For example, if you want to create a function that prints the word "Hello" on the screen and adds one to variable called total then the chunk of C code that you want to turn into a function is just:

printf("Hello");
total = total + l;

To turn it into a function you simply wrap the code in a pair of curly brackets to convert it into a single compound statement and write the name that you want to give it in front of the brackets:

demo()
{
printf("Hello");
total = total + 1;
}

Don't worry for now about the curved brackets after the function's name. Once you have defined your function you can use it within a program:

main()
{
demo();
}

In this program the instruction demo (); is entirely equivalent to writing out all of the statements in the function. Functions are useful here but they have a more important purpose. If you are creating a long program then functions allow you to split it into "bite-sized" chunks which you can work on in isolation. As every C programmer knows, "functions are the building blocks of programs."

Functions and Local Variables

A function is a complete program sub-unit in its own right and you can declare variables within it just as you can within the main program. If you look at the main program we have been using you will notice it is in fact a function that just happens to be called "main"! So to make demo work we have to add the declaration of the variable total:

demo()
{
int total;
printf("Hello");
total=total+1;
}

The total is only usable within the demo function is one thing - but notice that it only existed within this function, which is a more subtle point. The variables that a function declares are created when the function is started and destroyed when the function is finished. So if the intention is to use total to count the number of times the demo function is used - forget it! Each time demo is used the variable total is created afresh, and at the end of the function the variable goes up in a puff of smoke along with its value. So no matter how many times you run demo total will only ever reach a value of 1, assuming that it's initialised to 0.

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)
{
int result;
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.

Parameters are the main way of getting values into a function. 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.

For Example:
sum(int a, int b, int result)
{
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.

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:

return value;

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.

So now we can at last write the correct version of the sum function:

int sum(int a, int b)
{
int result;
result = a + b;
return result;
}

You can use a function anywhere that you can use a variable. For example,

r=sum(1,2)*3
is perfectly OK, as is
r=3+sum(1,2)/n-10

Obviously, the situation with respect to the number of inputs and outputs of a function isn't equal.

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:

sum(1,2);

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 function would exit and return to the calling program.

Functions and Prototypes

The only requirement is that the function's type has to be known before it is actually used. One way is to place the function definition earlier in the program than it is used - for example, before main(). The solution is to declare the function separately at the start of the program.

For example:
int sum();
main()
{
etc...

declares the name sum to be a function that returns an integer. By default if you don't declare a function before you use it then it is assumed to be an int function - which is usually, but not always, correct. It is worth getting into the habit of putting function declarations at the start of your programs because this makes them easier to convert to full ANSI C.