Functions in C++

Function Overloading in C++

One significant addition made to the capabilities of function in C++ is that of function overloading. With this facility you can have multiple functions with the same name, unlike C, where all the functions in a program must have unique names. In C, every function has to have a unique name. At times this becomes annoying. For example, there are following three different functions that return the absolute value of an argument: int abs (int i); long labs (long l); double fabs (double d); All these functions do the same thing, so it seems unnecessary to have three different function names. C++ overcomes this situation by allowing the programmer to create three different functions with the same name. This is called function overloading. Let us illustrate function overloading with an example as follows: int max (int, int); int max (int, int, int); //function prototypes int main() { cout << max(99,...

Inline Functions in C++

As we know, when we call a function from the main program, the control jumps to the function and then comes back to the calling program when execution of function is completed. This process, although looks simple, but involves a lot of background processes as of passing of parameters to the function, allocating of storage for function’s local variables, storing the current variables, etc. C++ provides a convenient way of omitting all these processes by simply declaring the function to be inline. When we declare a function inline, we tell the compiler to replace each call to such function(declared inline) with the explicit code of the function. To declare a function inline all we need to do is to add the word inline before the definition of the function as: inline data-type function_name(type 1 arg 1, type 2 arg 2, …, type n arg n); Also, in the function-prototype of...

Default Arguments in Functions

In C, if a function is defined to receive 2 arguments, whenever we call this function we have to pass 2 values to this function. If we pass one value then some garbage value is assumed for the last argument. As against this, functions in C++ have an ability to define default values for arguments that are not passed when the function call is made. The default arguments are given only in the function prototype and should not be repeated in the function definition. The compiler uses the prototype information to build a call, not the function definition. Let us illustrate this concept with an example: Here is an example of a prototype (i.e., function declaration) with default values: float amount(float principal, int period, float rate = 0.15); The default value is specified in a manner syntactically similar to the variable initialization. The above prototype declares a default value of...

Function Prototypes in C++

All the functions in C++ (which are used first and defined later in the program) need to be prototyped. Function prototypes are usually written at the beginning of the program, ahead of any programmer-defined functions (including main()). The general form of a function prototype is: data-type function_name(type 1 arg 1, type 2 arg 2, …, type n arg n); Where, data-type represents the data-type of the item that is returned by the function, function_name represents the function name, and type 1, type 2, …, type n represents the data-type of the arguments arg 1, arg 2, …, arg n. Notice that a function prototype resembles the first line of a function definition (though a function prototype ends with a semicolon). Advantages of Function Prototypes in C++: Use of function prototypes in C++ offers following advantages: Prototypes enables the compilers to provide stronger type checking Because of the use of prototypes,...

Accessing of Functions in C++

A function can be accessed (i.e., called) by specifying its name, followed by a list of arguments enclosed in parentheses and separated by commas as shown below. function_name(int a, int b); If the function call does not require any arguments, an empty pair of parentheses must follow the name of the function as shown below. function_name(); The function call may be a part of a single expression (such as an assignment statement), or it may be one of the operands within the more complex expression as shown below. int returnedvalue = function_name(); int c = 3 * function_name() + 5; The arguments appearing in the function call are referred to as actual arguments, in contrast to the formal arguments that appear in the first line of the function definition. In normal function call, there will be one actual argument for each formal argument. The actual arguments may be expressed as...

Defining of Functions in C++

A function definition has two principal components: the first line (including the argument declaration), and the body of the function In general, the first line can be written as: data-type function_name(type 1 arg 1, type 2 arg 2, …, type n arg n) where, data-type represents the data type of the item that is returned by the function, function_name represents the function name, and type 1, type 2, …, type n represents the data-type of the arguments arg 1, arg 2, …, arg n. The data-types are assumed to be of the type int if they are not shown explicitly. However, the omission of the data type is considered poor programming practice, even if the data items are integers. The arguments are called formal arguments, because they represent the names of the data items that are transferred into the function from the calling portion of the program. The names of...