Examples of Function Definitions

The following example contains a function declarator i_sort with table declared as a pointer to int and length declared as type int. Note that arrays as parameters are implicitly converted to a pointer to the element type.

/**
 ** This example illustrates function definitions.
 ** Note that arrays as parameters are implicitly
 ** converted to a pointer to the type.
 **/
 
#include <stdio.h>
 
void i_sort(int table[ ], int length);
 
int main(void)
{
   int table[ ]={1,5,8,4};
   int length=4;
   printf("length is %d\n",length);
   i_sort(table,length);
}
void i_sort(int table[ ], int length)
{
  int i, j, temp;
 
  for (i = 0; i < length -1; i++)
    for (j = i + 1; j < length; j++)
      if (table[i] > table[j])
      {
        temp = table[i];
        table[i] = table[j];
        table[j] = temp;
      }
}

The following are examples of function declarations (also called function prototypes):

double square(float x);
int area(int x,int y);
static char *search(char);

The following example illustrates how a typedef identifier can be used in a function declarator:

typedef struct tm_fmt { int minutes;
                      int hours;
                      char am_pm;
                    } struct_t;
long time_seconds(struct_t arrival)

The following function set_date declares a pointer to a structure of type date as a parameter. date_ptr has the storage class specifier register.

void set_date(register struct date *date_ptr)
{
  date_ptr->mon = 12;
  date_ptr->day = 25;
  date_ptr->year = 87;
}

C C99 requires at least one type specifier for each parameter in a declaration, which reduces the number of situations where the compiler behaves as if an implicit int were declared. Prior to C99, the type of b or c in the declaration of foo is ambiguous, and the compiler would assume an implicit int for both.

int foo( char a, b, c )
{
   /* statements */
}

Related References

IBM Copyright 2003