Old-style function declarations

The C programming language exists in two versions. The original version was developed by Kernighan & Ritchie for use in systems programming on Unix systems, and achieved considerable popularity. It was considerably revised and standardised by ANSI (American National Standards Institute) - the current standard was finalized in 1988 and formally approved by ANSI in 1989, which is why modern C is often referred to as "Ansi C", while the earlier version is often referred to as "Kernighan & Ritchie C".

In this course, you have been taught the current version of C. In most cases, the differences between the versions are slight and of marginal interest to the ordinary programmer, but one of the main differences lies in how functions are declared and defined.

An old-style function definition can look something like the following:
int oldfunc(a, b)
int a, b;
{
   ...
}
A new-style function definition looks something like the following:
int newfunc(int a, int b)
{
   ...
}

It is not instantly obvious from the above why the new style is better than the old one, but without going to deeply into the matter here, the main reason is that it permits better control over how function calls match the actual function definition. In the old style, all function parameters and return values default to type "int" if they are not explicitly declared before use, and there is no formal requirement to declare them. This can cause rather esoteric bugs which can't be detected by the compiler

In Ansi C, a function must be declared before it can be called. For small programs, such as the ones used in this course, this can be achieved by defining the function before the function call is used (which is why the main() function always comes last in our programming examples), but a more formally correct way of doing this is by including a function prototype at the start of the file for all functions used within the program. A function prototype looks very much like the start of the function definition, but without the function body, and ending with a semicolon. Something like the following:

#include <stdio.h>

/* Function prototypes */
double onefunc(double input);
void nextfunc(int *a, int *b);

main()
{
  double c, d;
  int x,y;
  ...
  c = onefunc(d);
  nextfunc(&x, &y);
  ...
  return;
}

/* Function definitions */
double onefunc(double input)
{
   double value;
   /* Do something */
   ...
   return value;
}
void nextfunc(int *a, int *b)
{
   /* Do something else */
   ...
   return; /* This is a function of type "void" and has no return value */
}

It is strongly recommended that you stick to Ansi C in all your programming, but there are occassions when you may encounter old-style function declarations. One such occassion was the original version of the sample exam paper which was handed out to a few of you ...


Back to main page
Author : Keith Halewood / Helge Nareid Current contact: Gorry Fairhurst G.Fairhurst@eng.abdn.ac.uk