By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,411 Members | 999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,411 IT Pros & Developers. It's quick & easy.

Function Overloading

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
"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

P: n/a

<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

P: n/a

<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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.