Functions 


C Programming Tutorial 

Go to the Table of Contents

 

Making black boxes. Solving problems. Getting results.

A function is a module or block of program code which deals with a particular task. Making functions is a way of isolating one block of code from other independent blocks of code. Functions serve two purposes. They allow a programmer to say: `this piece of code does a specific job which stands by itself and should not be mixed up with anyting else', and they make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text.

Functions help us to organize a program in a simple way; in Kernighan & Ritchie C they are always written in the following form:

identifier (parameter1,parameter2,..)

types of parameters

{ variable declarations

statements..
......
....

}
For example
Pythagoras(x,y,z)

double x,y,z;

{ double d;

d = sqrt(x*x+y*y+z*z);

printf("The distance to your point was %f\n",d);
}

In the newer ANSI standard, the same function is written slightly differently:

Pythagoras(double x, double y, double z)

{ double d;

d = sqrt(x*x+y*y+z*z);

printf("The distance to your point was %f\n",d);
}

You will probably see both styles in C programs.

Each function has a name or identifier by which is used to refer to it in a program. A function can accept a number of parameters or values which pass information from outside, and consists of a number of statements and declarations, enclosed by curly braces { }, which make up the doing part of the object. The declarations and `type of parameter' statements are formalities which will be described in good time.

The name of a function in C can be anything from a single letter to a long word. The name of a function must begin with an alphabetic letter or the underscore _ character but the other characters in the name can be chosen from the following groups:

a .. z
(any letter from a to z)
A .. Z
(any letter from A to Z)
0 .. 9
(any digit from 0 to 9)
_
(the underscore character)

This means that sensible names can easily be chosen for functions making a program easy to read. Here is a real example function which adds together two integer numbers a and b and prints the result c. All the variables are chosen to be integers to keep things simple and the result is printed out using the print-formatted function printf, from the the standard library, with a "%d" to indicate that it is printing a integer.

Add_Two_Numbers (a,b) /* Add a and b */

int a,b;

{ int c;

c = a + b;
printf ("%d",c);

}

Notice the position of the function name and where braces and semi-colons are placed: they are crucial. The details are quickly learned with practice and experience.

This function is not much use standing alone. It has to be called from somewhere. A function is called (i.e. control is passed to the function) by using its name with the usual brackets () to follow it, along with the values which are to be passed to the function:

main ()

{ int c,d;

c = 1;
d = 53;

Add_Two_Numbers (c,d);
Add_Two_Numbers (1,2);

}

The result of this program would be to print out the number 54 and then the number 3 and then stop. Here is a simple program which makes use of some functions in a playful way. The structure diagram shows how this can be visualized and the significance of the program `levels'. The idea is to illustrate the way in which the functions connect together: