answersLogoWhite

0

Functions are short procedures or subroutines that can be called as and when required. Unlike goto statements, which branch off to a labelled section of code never to return, functions always return back to the caller. Functions may also call other functions. Functions are typically used to reduce the amount of duplicate code, producing more consistent and error-free code and reducing maintenance whenever functions need to be modified. Functions are also used to make code more readable by replacing complex or compound statements with a simpler function call. By using easy to understand function names, code becomes self-documenting, reducing the need to include verbose comments that only serve to interrupt the flow of the code.

Although there is a performance penalty in calling functions as opposed to inline code, the compiler's optimisation routines can inline expand functions whenever there is an advantage in doing so. Typically this applies to short functions with one or two simple statements, but can also apply to functions that are seldom called.

Functions may also return a value back to the caller (a function that does not return a value simply returns void). Functions can modify their behaviour according to arguments supplied to the function. The number and type of arguments is dependant upon the function signature. The signature also differentiates between functions with the same name, thus allowing functions to be overloaded to cater for different arguments types.

If the argument types are references (or pointers), the function can modify the arguments passed to it unless the arguments are declared const. Passing by reference also allows functions to return more than one value to the caller (typically the return value itself is used to provide diagnostics, such as an error code).

If the arguments are passed by value, however, the function creates a local copy of the argument, thus preventing changes to the argument that was actually passed. If the argument being passed is an object, the object's copy constructor is called automatically. Since this can be detrimental to performance, objects should always be passed by reference unless the function actually needs to work on a copy of the object rather than the original object.

In C++, classes make use of functions, known as member methods, to provide the implementation of operations that may be applied to an object's data. Classes may also employ virtual functions that allow derived objects to provide more specialised implementations whilst retaining the existing generic code. This also helps to reduce duplicate code, but also allows objects to behave polymorphically. That is, it is not necessary for a generic class to know any of the implementation details of its derivatives. You simply call the generic method of the base class and the virtual table redirects the call to the most-specialised override of that function, if one exists. If not, the generic method is called instead. Classes can also make use of pure-virtual functions, such that the base class need not provide any generic implementation at all. These classes are abstract classes which cannot be instantiated by themselves. Instead, you are expected to derive new classes from abstract classes, and the derived classes must provide the implementation even if the base class provides a generic implementation. Only classes that provide or inherit a complete implementation of all pure-virtual methods can actually be instantiated.

User Avatar

Wiki User

11y ago

What else can I help you with?