Solved

function overloading

Posted on 2004-05-01
18
764 Views
Last Modified: 2010-04-02
Hi guys,
     is there a technical reason why C does not support overloading of functions, or is it just historical reasons? if so, why wasn't it introduced to the standard? is there any C implementation which supports function overloading?

Thanks
0
Comment
Question by:doggz
  • 6
  • 4
  • 3
  • +3
18 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
My definition of C: just a high-level assembly language. What you can use is the possibility to use function pointers, but only for your own functions.

Function overloading is possible though in C++. If you would consider that as a C-implementation...
0
 

Author Comment

by:doggz
Comment Utility
I mean function overloading for my own functions, not necessarily library once: foo(int), foo(int,int). why is it out of the standard? is it conecptual problem? implementation issues?
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
The issue here is not adding so much stuff into C so that it becomes C++

If you want to use overloading use C++.

There are many different languages for a reason .. else .. you can always add feature of language B to language A and make a super language and ultimately you can have one universal language which includes all .

/abhijit/
0
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
True, but it's even more funcdamental than that. As I said, C is high-level assembly, i.e. a C programme runs directly on a real CPU (originally PDP9, then PDP11). Features: pointers, indirect addressing, registers, arrays but no array-bound checking, memory allocation but not built-in into the language. The UNIX O/S will send you a signal when you do someting illegal. You don't run the program on a virtual machine, like you do with lots of other languages, e.g. Pascal. They stayed so close to the machine because they wanted to write an Operating System using the same language. Overloading is not important for Operating Systems and far beyond the scope of such a language.
0
 

Author Comment

by:doggz
Comment Utility
If designers of programming languages took your advice avizit, then all languages were bounded to their first interface. However programming and languages are dynamic, and thank God for that. Features introduced to one language are (unless contradicting with design or concepts of exisiting language) are entering other languages. For example ANSI C introduced many changes and additions to C standard. Adding function overloading to C does not make it a duplicate of C++, just as much as having function overloading in Java makes it a duplicate of C++. When desinging a programming language you consider a set of features you want the language to have and then implement them in your language implementation.
Having a feature missing in the language is not a reason for porting to another. It s like saying that is you don't have a word in English for something, which you can say in French, you suggest I'll use French instead of introducing a new word to English.
0
 
LVL 5

Expert Comment

by:dennis_george
Comment Utility
The main reason behind it is "name mangling"

The name mangling which c language follows doesn't allow us to implement function overloading.
i.e. in C the name mangling doesn't consider the parameter number and the parameter types.

But in C++ the name mangling is such that you can implement function overloading.

Dennis

0
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
What are the changes from the original C to the C we know as ANSI C? From the point of view of a programmer they might seem considerable, but with all due respect, the code generated stays the same. What's been altered is (correct me if I'm wrong ;)
- stronger type checking: a good thing, but it has little influence on code generation
- assignments like =+ and =- changed to += and -= (already in the K&R C language)

So, what's new?
0
 

Author Comment

by:doggz
Comment Utility
But that's exactly what I'm talking about. For the sake of the user, when it comes to function overloading it seems like an almost trivial issue, if it was added to the ANSI C you would say, whats the big deal, the generated code did not change a bit. So unless there were technical issues, in the sense of the generated code, as implied by dennis above, I assume they would have added it to the standard.

Dennis, can you please give a reference or write a little more on the name mangling?
thanks.
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
i would tend to agree with  sjef_bosman  that overloading would be far beyoind the scope of the C language .

plus its not only overloading , you can think of adding many more features
and freezing ( more or less ) the standard seems to be  good idea .. to avoid the feature explosion

 
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
A simple technical issue: a function func() is translated to assembler as a global label with the name "_func", i.e. there is a direct relationship of C-As. When linking object code, these names are all resolved. You can combine many C-object files (*.o) and libraries (object archives) into one executable. If overloading were permitted, how do you think this would be possible? Name resolution is a lot more difficult when overloading, and requires a lot more administration, for which the standard C linker is not equipped.
0
 

Author Comment

by:doggz
Comment Utility
So the reason in simple words is that  a function is represented by its name, and not as in C++ or other languages by its signature (name and arguments), and therefore this is problematic. IIRC when you assign a function value to a function pointer you assign it by the name of the function. This may be problematic if overloading was allowed.

thanks. I will leave the question open for sometime allowing other users to add their comments but I will give sjef_bosman the points.
0
 
LVL 2

Expert Comment

by:Avik77
Comment Utility
For an Object Oriented system we have data and functions encapsulated within what u call objects. These objects are unique. There are many objects which have many common methods like for eg. for a circle object there is a draw method, a square object there is also the draw method both taking the centre point as the parameter. Now by overloading we preserve the meaning of the methods/operations. Like both has to draw the figure, whether its a circle or a square, so why give different names to methods that does conceptually the same thing (to draw) ???
As u know C is not an object oriented prog lang. i.e it does not provide any feature by which programmers can represent objects, classify them or work in an OO Environment. So why the C compiler should be burdened with features like overloading where it itself supports procedural programming. A new concept here is a new function and not a new class of objects.

Avik.
0
 
LVL 5

Expert Comment

by:twobitadder
Comment Utility
>> So the reason in simple words is that  a function is represented by its name
Yes.

int funcA(int anInt)
{
   /*doStuff*/
}

int funcA(int anInt, anotherInt)
{
  /*doSomethingElse*/
}

funcA being assigned a memory address and the compiler keeping a mapping of function names to addresses, you mess up the compiler table because it assignes it based on "funcA".
It would seem trivial though to change the compiler so that it based its mapping on "funcA(int anInt)" or "funcA(int anInt, int anotherInt)".
0
 
LVL 5

Expert Comment

by:dennis_george
Comment Utility
0
 
LVL 5

Accepted Solution

by:
dennis_george earned 50 total points
Comment Utility
Hi,

The only technical difficulty in supporting function overloading in C is the name mangling followed by C and C++.

See, every code written in C when converted in to object file (intermediate language) it changes the name of the function (name mangling) to _functionname. i.e. in C and '_' is added to function name.

i.e. you have to change your compiler's name mangling scheme to cope with it. But if you do that you cannot make any library functions because the name mangled funcation name will differ.

I will give you an example.......

Say you want to use a existing C function in a C++ file... How will you do this ?

You cannot directly call that function....

you have to use 'extern C' before calling that function so as to follow the C name mangling scheme... otherwise it will follow the C++ name mangling scheme and inturn result in a unresolved linking error.


So you see if you want to implement function overloading in C.... what all should be changed... your name mangling techniques... your previously written codes i.e. compatibility with previous codes... and so on.....

Just go through the links I have provided and give a thought on what I said....

Have a nice day
Dennis
0
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
This is not an answer to the original question, being "is there a technical reason why C does not support overloading of functions, or is it just historical reasons". It just shows how they done it for C++, which looks a lot like C but is an entirely different language.
0
 
LVL 2

Expert Comment

by:Avik77
Comment Utility
>> but I will give sjef_bosman the points.
<LOL>
0
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
Wait till we meet in the street :-||

LOL too
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now