Function Overloading

K

kelvSYC

In C, can you make functions like this:

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

Tristan Miller

Greetings.

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

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?
 
S

Sheldon Simms

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
 
N

no

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

Mike Wahler

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
 
N

no

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?
 
M

Malcolm

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".
 
B

Ben Pfaff

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?
 
J

Joona I Palaste

Ben Pfaff said:
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 ([email protected]) ---------------------------\
| 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"
 
M

Micah Cowan

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
 
G

Guest

Nonsense.

Be kind to me. I am just learning.
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? :)
 
J

Joona I Palaste

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 ([email protected]) ---------------------------\
| 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
 
J

Joona I Palaste

One rude or incorrect behavior does not justify another.

Tell that to Clockmeister.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top