Category: Functions in C++

Interested in learning C Programming? Click here...


Function Overloading in Cpp 0

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:

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...

Inline Functions in Cpp 4

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)...


Default Arguments in Functions 0

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...

Function-Prototypes-in-Cpp 1

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:

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...