answersLogoWhite

0


Best Answer

The need for function overloading is to allow the same function call to accept different parameter types and/or a different number of parameters. The function signature differentiates the functions. The return type is also part of the signature, and needn't be the same for every overload.

Where the function signature is largely the same, with the same count of parameters but a different parameter type and return type, template functions can provide an alternative method of creating the function overloads. The compiler generates the necessary code for you, on an as-required basis, so you only need to write the function once, rather than once for each type.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Function overloading permits you to call the same function with differing numbers and types of arguments and return types, thus increasing the flexibility of the function.

The area of a rectangle is calculated from the product of the width by the length. Therefore we can use the following function:

const int area(const int width, const int length) { return( width * length ); }

However, suppose we declare the following structure:

struct RECT

{

int width;

int length;

};

To calculate the area we must call the area function as follows:

RECT rect; rect.width = 5; rect.length = 10;

int area = area( rect.width, rect.length );

However, if we overload the function we can cater for the structure itself:

const int area(const RECT& rect) { return( area( rect.width, rect.length )); }

Thus the call can be reduced to:

RECT rect; rect.width=5; rect.length=10;

int area = area( rect );

Now we have two ways of calling the same function, depending on whether we have a structure or not. Ultimately, the function calls are simplified.

A common example of function overloading is the max function:

int max(int x, int y) { return( x>y?x:y ); }

This returns the greater of two integers.

To cater for different types, we must write separate functions for each type. for instance, to cater for float types, we'd use the following overload:

float max(float x, float y) { return( x>y?x:y ); }

However, because the implementations would be exactly the same regardless of the type, C++ allows us to enlist the compiler to generate the overloads on our behalf, using just one function template:

template <typename _T>

_T const& max(_T const& x, _T const& y) { return( x>y?x:y ); }

In point of fact, we don't actually need this specific template because it already exists (std::max). However, we can extend the basic functionality further by overloading the template function. For instance, suppose we need to frequently find the maximum of three data types. We could call std::max( std::max( x, y ), z ) each time we need to, but we can greatly simplify our code simply by overloading the std::max function with yet another template function:

template <typename _T>

_T const& max( _T const& x, _T const& y _T const& z ) { return( std::max( std::max( x, y ), z )); }

Thus we can extend the functionality further to cater for any number of arguments.

But suppose we have an array and want to return the maximum element in that array. For that we'd need yet another overload, passing a pointer to the starting address of the array along with the length of the array:

template <typename _T>

_T const max( _T const* arr, int length )

{

//

int r = arr[0], i = 1;

while( i < length )

r = std::max( r, arr[i++] );

return( r );

};

Thus we now have one function name which can cater for just about any type we might care to process, provided an appropriate signature exists. And where a suitable signature does not exist, we can easily create one. Function templates allow us to minimise code duplication thus reducing code maintenance, which makes it much easier to modify the implementation (should we need to) in one place, rather than in several places.

The alternative to function overloading (and therefore function templates) would be to provide different function names to cater for each signature, along with individual implementations, even if the implementations are exactly the same. Apart from increasing code duplication and maintenance, the programmer must remember the exact names of each version of the function in order to make the correct call. with function overloading, there is only one function name to remember. The compiler can work out which version of the function to call based on the number and type of arguments passed to the function.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Need of function overloading in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is function overloading in c language of computer?

There is no such thing as function overloading in C; that is a feature of C++. Function overloading allows us to provide two or more implementations of the same function. Typically, we use function overloading so that the same function can cater for different types. For instance, we might provide one implementation that is optimised to handle an integer argument while another is optimised to handle a real argument. We can also use function overloading to provide a common implementation of a function which can then be invoked by overloads that handle the low-level type conversions.


Why do you use of operator overloading in c?

Overloading, Overriding, Polymorphism, Information Hiding, Inheritance all these are CONCEPTS of C++ and Java. An Object Oriented Language and not of C language. Thats why Bjarne Stroustrup came up with C++ ...


Why use new and delete operator overloading in c plus plus?

one reason to use new and delete operator overloading in c++ is when you are using your own memory manager code. when the user of your code calls the new keywork, your memory manager code can allocate memory.


What are the drawbacks of function overloading?

None whatsoever. There is always the chance you will end up creating overloaded functions that are never used, but your compiler should be able to eliminate these for you (unreachable code). Otherwise there is no harm in having them in your source code, especially if the code is intended to be re-usable. For those that are functionally the same, the only difference being the type of parameter, template functions are a far better solution to overloading for every conceivable type. The compiler will generate the overloads for you, on an as-required basis, and you only have the one function to maintain.


A c plus plus statement that invokes a function is known as?

...a function call.

Related questions

Is it possible to do operator overloading in c?

No. Operator and/or function overloading is only a C++ thing.


What is the c plus plus program to calculate the area of a circle using function overloading?

Function overloading is used when you want to re-use the same function name with different argument types or a different number of arguments. Calculating the area of a circle isn't the sort of function that requires overloading since the only argument you need is the radius. double area_of_circle (const double radius) { const double pi=4*atan(1); return pi*radius*radius; }


What is function overloading in c language of computer?

There is no such thing as function overloading in C; that is a feature of C++. Function overloading allows us to provide two or more implementations of the same function. Typically, we use function overloading so that the same function can cater for different types. For instance, we might provide one implementation that is optimised to handle an integer argument while another is optimised to handle a real argument. We can also use function overloading to provide a common implementation of a function which can then be invoked by overloads that handle the low-level type conversions.


What is an operator overloading in C?

one function but multiple behaviours depending on the parameters


Why do you use of operator overloading in c?

Overloading, Overriding, Polymorphism, Information Hiding, Inheritance all these are CONCEPTS of C++ and Java. An Object Oriented Language and not of C language. Thats why Bjarne Stroustrup came up with C++ ...


What is the operator of power in c plus plus?

There is no "power" operator in C or C++. You need to the use the math library function pow().


What are the building function in c plus plus?

There is no such term as "building function" in C++.


How do you correct the C plus plus programming error missing function header?

You need to #include the header file that contains the missing function's declaration.


Why use new and delete operator overloading in c plus plus?

one reason to use new and delete operator overloading in c++ is when you are using your own memory manager code. when the user of your code calls the new keywork, your memory manager code can allocate memory.


What are the drawbacks of function overloading?

None whatsoever. There is always the chance you will end up creating overloaded functions that are never used, but your compiler should be able to eliminate these for you (unreachable code). Otherwise there is no harm in having them in your source code, especially if the code is intended to be re-usable. For those that are functionally the same, the only difference being the type of parameter, template functions are a far better solution to overloading for every conceivable type. The compiler will generate the overloads for you, on an as-required basis, and you only have the one function to maintain.


A c plus plus statement that invokes a function is known as?

...a function call.


When will you make a function inline in c plus plus?

yes,we can make function inline