A function must be defined once and can be called any number of times. A function has a name, arguments, and a body. A common problem with programs is that a function call can not be matched up to any function in the rest of the program.
The body is compiled and stored in memory. When the call is executed the computer jumps to the start of the body. Once in the body it executes its code until it gets to the end or a 'return' statement. If it executes an "exit" statement the whole program terminates. If the computer gets to a return or the end of the function then it jumps to the instruction in the program immediately after the call.
The Main Function
All executable programs must have a special function with name
"main" and special arguments. The program run starts when the
operating system calls this "main" function.
The main program should return an positive int that indicates if any errors have been discovered by the program. The number zero indicates that zero errors have occurred. A number other than zero indicates that the program did not complete its task and the precise number often is used to indicate the reason for the program failing. The operating system has the power to find out this returned number.
Compilation
The compiler
has to match up the function call with the right function. It
uses
the name of the function plus the number and type of the
arguments
in the call. to find a matching function. If no function is
found that has the right name and arguments then the compiler
reports an error and the program can not be run.
A function definition has two parts: a header and a body. The header specifies how the function can be called. The body defines what happens if and when the function is called. We say that the header specifies the function. The complete body and header is said to be an implementation of the function.
In C++ there is a special syntax for giving the header without defining the body: The body is replaced by a semicolon. The result is called a function prototype, or specification. A file that contains a series of function prototypes is called a header file. Typically a compiler learns about the functions you plan to use in a program file by compiling a series of these header files #included at the start of the program. These are often predefined library files that come with the compiler. Here, for example, [ cmath.txt ] is a simplified version of our own <cmath> library.
A call by reference is shown in the header by adding an ampersand symbol after the type:
type name (...., type & argument, ....)
However this also permits the function to change the arguments. This can make it difficult to find out what is going on in a program. It is therefore possible to indicate that an argument is called by reference but is safe from being changed:
type name (...., const type & argument, ....)
. . . . . . . . . ( end of section Handling arguments) <<Contents | End>>
Return Values
A function header can specify that a value must and will
be returned by a function. If so there must be one or
more return_statement's that contain an expression. If the
expression is executed than the expression is evaluated
and the value left behind (on the runtime stack) for the calling
program to collect and use.
Overloading
A C++ function is matched, by the compiler+linker, to
its calls by using both its name and the types of its arguments.
So the same name can be used for several similar functions with
different types of data.
This works well as long as the names you choose actually reflect what the functions do.
. . . . . . . . . ( end of section Introducing Functions in C++) <<Contents | End>>
Abreviations