473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Function Overloading

In C, can you make functions like this:

int foo(int a);
int foo(int a, char b);
....
etc.

--
I am only a mirage.
Nov 13 '05 #1
13 10477
kelvSYC <ke*****@no.email.shaw.ca> writes:
In C, can you make functions like this:

int foo(int a);
int foo(int a, char b);


No.
--
"Am I missing something?"
--Dan Pop
Nov 13 '05 #2
Greetings.

In article <02************************@no.email.shaw.ca>, kelvSYC wrote:
In C, can you make functions like this:

int foo(int a);
int foo(int a, char b);
...
etc.


Not in standard C, though your compiler may provide this functionality as a
non-standard extension. C++, as you're probably aware, does allow function
polymorphism.

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 13 '05 #3

"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@pfaff.stanford.edu...
kelvSYC <ke*****@no.email.shaw.ca> writes:
In C, can you make functions like this:

int foo(int a);
int foo(int a, char b);


No.


Sadly not! ...... Options? Take a lead from the printf family -
variant names and/or va_args, pass a structure or use C++.
Nov 13 '05 #4
On Sat, 04 Oct 2003 10:59:35 +0000, no@no.invalid wrote:

On 4-Oct-2003, "James Harris" <no.email.please> wrote:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@pfaff.stanford.edu...
> kelvSYC <ke*****@no.email.shaw.ca> writes:
>
> > In C, can you make functions like this:
> >
> > int foo(int a);
> > int foo(int a, char b);
>
> No.


Sadly not! ......


It has it's own advantages and disadvantages. C code turns out to make
smaller and more efficient executables when compared with C++, right?


if so, then it's not because C lacks overloading of function names.

-Sheldon

Nov 13 '05 #5
"no@no.invalid" <no@no.invalid> writes:
I think overloading of functions *does* take up some extra
memory when compared to using 2 functions with different names.


I'd be very surprised. Why do you think so?
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
Nov 13 '05 #6

<no@no.invalid> wrote in message
news:20***********************@mail.zedz.net...

On 4-Oct-2003, Sheldon Simms <sh**********@yahoo.com> wrote:
On Sat, 04 Oct 2003 10:59:35 +0000, no@no.invalid wrote:

On 4-Oct-2003, "James Harris" <no.email.please> wrote:

> "Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
> news:87************@pfaff.stanford.edu...
> > kelvSYC <ke*****@no.email.shaw.ca> writes:
> >
> > > In C, can you make functions like this:
> > >
> > > int foo(int a);
> > > int foo(int a, char b);
> >
> > No.
>
> Sadly not! ......

It has it's own advantages and disadvantages. C code turns out to make
smaller and more efficient executables when compared with C++, right?


if so, then it's not because C lacks overloading of function names.


I think overloading of functions *does* take up some extra memory when
compared
to using 2 functions with different names.


IMO you think wrong. A C++ function overload *is*
a function with a different name. C++ simply
'hides' this fact.

-Mike
Nov 13 '05 #7

<no@no.invalid> wrote in message
IMO you think wrong. A C++ function overload *is*
a function with a different name. C++ simply
'hides' this fact.
Yes, but in doing so it takes up some extra steps which in turn results in
comparatively slower execution and more memory. Overloaded
function resolution is not as simple as the programmer himself naming
the functions differently.

It compensates this by features such as dynamic binding and hence may >

load the function into memory only when required.
BTW do the latest C compilers use dynamic binding?

C++ allows this

double root(double x);
double root(Quaternion q);

so you can take the root of a real or a quaternion using natural function
names. The C++ compiler will implement this by producing two functions with
mangled names, eg root_d and root_Quaternion. There is no runtime penalty
compared with writning two C functions with slightly different names.

However C++ also allows this

class number
{
public:
virtual void root(void) = 0;
};

Now we can declare "real" and "quaternion" as objects of type "number", and
provide for each a routine to reduce them to a square root.

The program decides what type "number" is a run time, and accesses the
function "root" through a pointer. There is therefore a speed and time
penalty compared with providing two directly-called C functions, though of
course the ability to use object-oriented design arguably compensates for
this.

Some platforms also put commonly-used library functions in separate modules
from the main executable. The OS may provide some sort of intelligent
management by only loading those functions which are needed at the time.
This has nothing to do with run-time binding in the C++ sense.

To answer the last question, you would have to clarify in what sense you are
using the term "dynamic binding".
Nov 13 '05 #8
"no@no.invalid" <no@no.invalid> writes:
On 6-Oct-2003, "Mike Wahler" <mk******@mkwahler.net> wrote:
<no@no.invalid> wrote in message
news:20***********************@mail.zedz.net...

On 4-Oct-2003, Sheldon Simms <sh**********@yahoo.com> wrote:

> On Sat, 04 Oct 2003 10:59:35 +0000, no@no.invalid wrote:
>
> >
> > On 4-Oct-2003, "James Harris" <no.email.please> wrote:
> >
> >> "Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
> >> news:87************@pfaff.stanford.edu...
> >> > kelvSYC <ke*****@no.email.shaw.ca> writes:
> >> >
> >> > > In C, can you make functions like this:
> >> > >
> >> > > int foo(int a);
> >> > > int foo(int a, char b);
> >> >
> >> > No.
> >>
> >> Sadly not! ......
> >
> > It has it's own advantages and disadvantages. C code turns out to
> > make
> > smaller and more efficient executables when compared with C++,
> > right?
>
> if so, then it's not because C lacks overloading of function names.

I think overloading of functions *does* take up some extra memory when
compared
to using 2 functions with different names.
IMO you think wrong. A C++ function overload *is*
a function with a different name. C++ simply
'hides' this fact.


Yes, but in doing so it takes up some extra steps which in turn results in
comparatively slower execution and more memory. Overloaded function
resolution is not as simple as the programmer himself naming the functions
differently.


Overload resolution normally takes place at compile time, not
runtime.
It compensates this by features such as dynamic binding and hence may load
the function into memory only when required.
Dynamic binding has nothing to do with function overloading.
BTW do the latest C compilers use dynamic binding?
No.









Would you please cut it out with the 20-plus blank lines at the
end of every article?
--
"It wouldn't be a new C standard if it didn't give a
new meaning to the word `static'."
--Peter Seebach on C99
Nov 13 '05 #9
Ben Pfaff <bl*@cs.stanford.edu> scribbled the following:
Would you please cut it out with the 20-plus blank lines at the
end of every article?


But Ben! I have a 6-line signature! Surely that gives everyone else the
right to do whatever they want to?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
- Guardian in "Jinxter"
Nov 13 '05 #10
"no@no.invalid" <no@no.invalid> writes:
On 6-Oct-2003, "Mike Wahler" <mk******@mkwahler.net> wrote:
I think overloading of functions *does* take up some extra memory when
compared
to using 2 functions with different names.


IMO you think wrong. A C++ function overload *is*
a function with a different name. C++ simply
'hides' this fact.


Yes, but in doing so it takes up some extra steps which in turn results in
comparatively slower execution and more memory.


Nonsense. Any extra steps/slower execution happens in the
*compiling* phase, not affecting the actual execution of your
particular application (I have yet to hear of a conforming C++
interpreter...), and will still happen much quicker than the
time it takes you to write out two distinct function names.

-Micah
Nov 13 '05 #11
no****@nospam.invalid <no****@nospam.invalid> scribbled the following:
On 6-Oct-2003, Micah Cowan <mi***@cowan.name> wrote:
(snip)
Any extra steps/slower execution happens in the
*compiling* phase, not affecting the actual execution of your
particular application (I have yet to hear of a conforming C++
interpreter...), and will still happen much quicker than the
time it takes you to write out two distinct function names.

I've heard that a "hello" world program on C++ takes up more space when
compared with C.
Is that wrong? :)


Generally speaking, yes. It is also wrong that a "hello" world program
on C++ takes up *less* space when compared with C.
Neither the C standard or the C++ standard specify *anything* about how
much space the program should take. Therefore any claim that "A {C,C++}
program takes up {more,less} space than a <insert language here>
program" is invalid.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"To err is human. To really louse things up takes a computer."
- Anon
Nov 13 '05 #12
"no@no.invalid" <no@no.invalid> writes:
On 6-Oct-2003, Ben Pfaff <bl*@cs.stanford.edu> wrote:
Would you please cut it out with the 20-plus blank lines at the
end of every article?


Why don't you object those who use OE and break quotes instead?


One rude or incorrect behavior does not justify another.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 13 '05 #13
Ben Pfaff <bl*@cs.stanford.edu> scribbled the following:
"no@no.invalid" <no@no.invalid> writes:
On 6-Oct-2003, Ben Pfaff <bl*@cs.stanford.edu> wrote:
> Would you please cut it out with the 20-plus blank lines at the
> end of every article?
Why don't you object those who use OE and break quotes instead?

One rude or incorrect behavior does not justify another.


Tell that to Clockmeister.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"We sorcerers don't like to eat our words, so to say."
- Sparrowhawk
Nov 13 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Doran_Dermot | last post by:
Hi All, I've seen lots of code in which the attributes of a class are accessed and modified using two separate methods. For example: class Problems: def __init__( self, refNum ):...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
7
by: asdf | last post by:
They looks so similar. Anybody would like to tell me their differences? Thanks a lot.
15
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
10
by: subramanian100in | last post by:
The following is a beginner's question. Suppose TYPE1 and TYPE2 are two types for which suitable ctors and operator= are defined. Suppose I have class Test { TYPE1 mem1;
10
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
14
by: mesut | last post by:
hi colleagues, I don't know if this is the right group for but it's in C# so I try. I have a #3 procedural function called GetInfo.. and those are 3 overloaded methods. I would like to use the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.