answersLogoWhite

0


Best Answer

Short answer: You can't.

Long answer: Function overloading is when you define two functions with the same name that take different arguments, e.g.:

void pickNose(int fingerId);

void pickNose(Finger finger);

This is a valid construct in C++, which supports function overloading. If you try this in C, you'll get some error about function already defined.

User Avatar

Wiki User

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

Wiki User

10y ago

Function overloading allows you to use the same function name with different parameter types. For instance, to determine the larger of two integers you might create the following function:

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

If you also wanted to determine the larger of two floats, then you need to overload the max function to accept floats:

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

Note that the only difference between these two functions is the parameter types and the return type. The compiler uses the parameter types to determine which of the two functions you are calling: if you pass two ints, the first overload is called, and if you pass two floats, the second is called.

In order to eliminate ambiguity, each overload must differ in the number or type of parameters passed. The exact combination of parameters is what distinguishes one function from another with the same name. This is known as the function signature. The return type plays no part in the signature, thus overloads cannot differ by return type alone.

Although the implementation of these functions is exactly the same, this needn't be the case for all overloads. If you wanted to determine the largest of three integers, for example, you could provide yet another overload:

int max(int x, int y, int z ){ return( max( max(x, y), z)); }

This overload calls the original version of the function twice, first to determine the largest of x and y, the result of which is then passed to the second call along with z, thus returning the largest of all three.

Note that function overloads must reside in the same namespace in order to be classed overloads. Functions with the same name but in separate namespaces are not overloads: thus it is possible to have two functions with exactly the same signature and return type. However, if both namespaces are in scope then the compiler cannot differentiate between them unless you explicitly resolve the ambiguity using the namespace resolution operator (::).

C++ also supports the concept of function templates. This is extremely useful when you have multiple overloads with the exact same implementation (as per the first int and float overloads shown above). Instead of manually coding all the possible variations you might need in order to cater for all types (including other primitives such as char or double, as well as user-defined types), you can define one template function for them all. The compiler will then generate the overloads for you, as and when they are required. Thus the max() function can be overloaded automatically with the following template:

template<class T>

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

Here, T represents a single type, whether it be int, char, double or a user-defined type. So long as the type supports the greater-than operator (>), the compiler will generate the appropriate overload for you. Therefore all you have to do is call the function with an appropriate type.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is overloading function in c and explain with example?
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.


Explain pointer to function with example c language?

It isn't a question, sorry.


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 Polymorphism and inheritance explain with example?

Polymorphism, is an object-oriented programming concept, which relates to the ability to create a variable, function or an object that has more than one form. This allows the object to invoke the correct instance of the variable, function or other object based upon the object type. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Here are some links to examples: C++: http://www.cplusplus.com/forum/beginner/10884/ c#: http://msdn.microsoft.com/en-us/library/ms173152.aspx Python: http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Java: http://www.tutorialspoint.com/java/java_polymorphism.htm


What is a sizeof function in C programming?

Sizeof is an example.

Related questions

Is it possible to do operator overloading in c?

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


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


Explain pointer to function with example c language?

It isn't a question, sorry.


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


C coding for operator overloading?

C does not support operator overloading. If you mean C++ operator overloading, it depends on exactly what you wanted to do. If you wanted to '+' to strings, then you could write: string operator+(string a, string b) { // do something }


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; }


Which type of function in c?

Try to be more precise; explain what you mean by the type of a function.


What is Polymorphism and inheritance explain with example?

Polymorphism, is an object-oriented programming concept, which relates to the ability to create a variable, function or an object that has more than one form. This allows the object to invoke the correct instance of the variable, function or other object based upon the object type. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Here are some links to examples: C++: http://www.cplusplus.com/forum/beginner/10884/ c#: http://msdn.microsoft.com/en-us/library/ms173152.aspx Python: http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Java: http://www.tutorialspoint.com/java/java_polymorphism.htm


What is a sizeof function in C programming?

Sizeof is an example.


Difference between formatted and unformatted function in c with example?

Disks and partitions should be formatted before usage.


Explain the following tearm in the context of object oriented programming Also explain how these concept are implement in c by given an example program for each?

Explain the following terms in the context of object oriented programming. Also explain how these concepts are implemented in C++ by giving an example program for each.