A Practical Introduction to the C Language for Computational Chemistry. Part 4

Controlling complexity is the essence of computer programming.

Brian W. Kernighan. in Software Tools (with PJ Plauger), 1976.


A C program is a collection of functions. A C function is equivalent to the subroutiing in FORTRAN or BASIC and procedures in PASCAL, PERL or PYTHON programming languages. It is a portion of the program that cannot be executed indipendently but only as part of another program. The function contains a specific algorithm or a stand alone procedure. You have already used several library functions in your previous programs. Output command for priting or reading files (such as printf(), openf()), mathematical functions (sqrt(), cos() are library or intrinsic functions as well. Other libraries functions, we can classify them as follows

  • Input/output functions. Input/output on the computer devices (e.g. output to the terminal, printer, hard disk, input from keyboard). It is usually used with #include <stdio.h>;
  • String manipulation functions. This library contains common operation on strings (e.g. concatenation, length, search and extraction of substrings). It is usually used with #include <string.h>;
  • Mathematical functions. Mathematical calculations (e.g. trigonometrics functions, exponentiation, square root extraction). It is usually used with #include <math.h>;
  • Graphical functions. Function for graphics operations (open a graphical window and canvas) and drawing graphical primitives (e.g. points, line, curves).
  • Operative system control functions. Operation requiriing allocation of the computer resources or devices (e.g. date and time, allocation of memory). It is usually used with, for example, #include <time.h>;
  • Data conversion functions. Operation for data conversion (e.g. change characters type, ascii to integer). It is usually used with #include <ctype.h>;

To use these function, you need to use the precompiler instruction #include at the beginning of the program. The compiler use by deafult the standard library #include <stdlib.h>;

You can write your own functions and it is called user defined functions. The use of function allow to structure the program and make easier its organization and reading. C language is structured around the use of functions. The function main is a function itself and it contains calls to other functions. both intrinsic and user defined functions.

Continue reading