HP-UX HB v13.00 Ch-11 - Software Development

HP-UX Handbook Rev 13.00 Page 6 (of 101)
Chapter 11 Software Development
October 29, 2013
This is a declaration of a function named main which takes no argument and returns a value of
type int. To create the symbol a definition is required:
int main() {
:
/* function code */
:
}
The definition of a function contains the function body. It must specify the same arguments and
return code as the declaration. A symbol definition implies the declaration, so if there is a symbol
definition in a source, no separate declaration is required.
The separation of definitions and declarations allows encapsulation and protection of source
code. To reference a symbol only the declaration is needed, so we can hide what's going on
inside:
int x(int);
int main() {
return x(1);
}
Function x() might be defined somewhere else, but we can call it here because we have its
declaration.
Source Code Organization
Usually there are two kinds of files that reflect the programmatical conventions above. Source
files contain symbol definitions. Rather than inserting symbol declarations in all source files,
they are collected in header files. The declarations in the header files can then be used by
referencing the header files. Header files in turn can also reference other header files.
$ cat HelloWorld.c
#include <stdio.h>
int main() {
printf("Hello World!\n");
}
$
The file HelloWorld.c is a source file and we will use it as our test case for the rest of the
course. It references the header file stdio.h and defines the function main().