Node:Functions, Next:, Previous:The form of a C program, Up:Top



Functions

Solving problems and getting results.

A function is a section of program code that performs a particular task. Making functions is a way of isolating one section of code from other independent sections. Functions allow a programmer to separate code by its purpose, and make a section of code reusable -- that is, make it so the section can be called in many different contexts.

Functions should be written in the following form:

type function_name (type parameter1_name, type parameter2_name, ...)

{
  variable declarations

  statements
  ...
  ...
  ...
}

You may notice when reading the examples in this chapter that this format is somewhat different from the one we have used so far. This format conforms to the ANSI Standard and is better C. The other way is old-fashioned C, although GCC will still compile it. Nevertheless, GCC is not guaranteed to do so in the future, and we will use ANSI Standard C in this text from now on.

As shown above, a function can have a number of parameters, or pieces of information from outside, and the function's body consists of a number of declarations and statements, enclosed by curly brackets: {...}.