The basic C++ language is relatively primitive. Its basic data types (integers, reals and characters) and operations on them (addition, subtraction, etc). are limited. To build complex programs one needs to use the module building features of c++. Modules are a way of packaging the basic data types and operations into useful and problem specific data types and operations.
C++ modules are called classes and functions. This topic covers functions.
A function is a packaging of C++ statements into a unit which called as needed from elsewhere in the program.
Every C++ program has at least one function - it is called
main
.
The same function can be called many times in the same or even different programs. Thus functions are reusable pieces of program.
The job that a function does usually abstracts some part of the problem and thus becomes a convenient building block for programming complex systems.
Functions extend the things that a C++ program can do. Many
commonly used functions are placed in libraries and accessed through
their header files (e.g."#include <iostream>
"
accesses the common and useful functions for doing I/O).
The identification of "good" functions (and classes) is a critical part of the art of programming. Good functions lead to programs that are easier to write and maintain. Bad functions lead to programs which are difficult to understand and thus prone to failures.
Functions are program modules which are called by other parts of the program. Functions and their calling programs communicate information in various ways.
Input parameters are data objects which are passed from the calling program to the function.
Output parameters> are data objects which are passed from the function to the calling program.
The return value is a single data object which is returned as the value of the function.
Global variables are variables which can be accessed by both the function and the calling program. Since the setting of global variables and their use can be difficult to determine when examining the function call, their use is tricky and is general discouraged as bad programming practice. Such data should be passed via parameters instead.
In some cases, this will result in an uncomfortably large number of parameters. Later we will look at structured data which can be used to group data items together and at classes, which can also pass data to member functions via data members.
A function has the following parts
any valid c++ identifier.
any data type or class name. void
is used to denote that this function does not return a
value
an ordered list giving the types of the parameters.
a compound statement containing the program that defines or implements the function.
A function prototype consists of the return-type function-name and parameter-list.
A function's prototype is used by the compiler to set up the function call without knowing how the function is defined.
This is very important because it allows functions to be defined separately and put into libraries for common use.
Header files consist mainly of function prototypes (including member functions of classes).
A function >signature consists of the function-name and parameter-list.
A function signature is used by the compiler for handling function overload
In the Forum: