Function Overloading

Discussion in 'C Programming' started by kelvSYC, Oct 3, 2003.

  1. kelvSYC

    kelvSYC Guest

    In C, can you make functions like this:

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

    --
    I am only a mirage.
    kelvSYC, Oct 3, 2003
    #1
    1. Advertising

  2. kelvSYC

    Ben Pfaff Guest

    kelvSYC <> 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
    Ben Pfaff, Oct 3, 2003
    #2
    1. Advertising

  3. Greetings.

    In article <021020032236313486%>, 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
    Tristan Miller, Oct 3, 2003
    #3
  4. kelvSYC

    James Harris Guest

    "Ben Pfaff" <> wrote in message
    news:...
    > kelvSYC <> 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++.
    James Harris, Oct 4, 2003
    #4
  5. kelvSYC

    Guest

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

    > "Ben Pfaff" <> wrote in message
    > news:...
    > > kelvSYC <> 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?
    , Oct 4, 2003
    #5
  6. On Sat, 04 Oct 2003 10:59:35 +0000, lid wrote:

    >
    > On 4-Oct-2003, "James Harris" <no.email.please> wrote:
    >
    >> "Ben Pfaff" <> wrote in message
    >> news:...
    >> > kelvSYC <> 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
    Sheldon Simms, Oct 4, 2003
    #6
  7. kelvSYC

    Guest

    On 4-Oct-2003, Sheldon Simms <> wrote:

    > On Sat, 04 Oct 2003 10:59:35 +0000, lid wrote:
    >
    > >
    > > On 4-Oct-2003, "James Harris" <no.email.please> wrote:
    > >
    > >> "Ben Pfaff" <> wrote in message
    > >> news:...
    > >> > kelvSYC <> 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.
    , Oct 4, 2003
    #7
  8. kelvSYC

    Ben Pfaff Guest

    "" <> 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
    Ben Pfaff, Oct 4, 2003
    #8
  9. kelvSYC

    Mike Wahler Guest

    <> wrote in message
    news:...
    >
    > On 4-Oct-2003, Sheldon Simms <> wrote:
    >
    > > On Sat, 04 Oct 2003 10:59:35 +0000, lid wrote:
    > >
    > > >
    > > > On 4-Oct-2003, "James Harris" <no.email.please> wrote:
    > > >
    > > >> "Ben Pfaff" <> wrote in message
    > > >> news:...
    > > >> > kelvSYC <> 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
    Mike Wahler, Oct 5, 2003
    #9
  10. kelvSYC

    Guest

    On 6-Oct-2003, "Mike Wahler" <> wrote:

    > <> wrote in message
    > news:...
    > >
    > > On 4-Oct-2003, Sheldon Simms <> wrote:
    > >
    > > > On Sat, 04 Oct 2003 10:59:35 +0000, lid wrote:
    > > >
    > > > >
    > > > > On 4-Oct-2003, "James Harris" <no.email.please> wrote:
    > > > >
    > > > >> "Ben Pfaff" <> wrote in message
    > > > >> news:...
    > > > >> > kelvSYC <> 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.

    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?
    , Oct 5, 2003
    #10
  11. kelvSYC

    Malcolm Guest

    <> 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".
    Malcolm, Oct 5, 2003
    #11
  12. kelvSYC

    Ben Pfaff Guest

    "" <> writes:

    > On 6-Oct-2003, "Mike Wahler" <> wrote:
    >
    > > <> wrote in message
    > > news:...
    > > >
    > > > On 4-Oct-2003, Sheldon Simms <> wrote:
    > > >
    > > > > On Sat, 04 Oct 2003 10:59:35 +0000, lid wrote:
    > > > >
    > > > > >
    > > > > > On 4-Oct-2003, "James Harris" <no.email.please> wrote:
    > > > > >
    > > > > >> "Ben Pfaff" <> wrote in message
    > > > > >> news:...
    > > > > >> > kelvSYC <> 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
    Ben Pfaff, Oct 6, 2003
    #12
  13. Ben Pfaff <> 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 () ---------------------------\
    | 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"
    Joona I Palaste, Oct 6, 2003
    #13
  14. kelvSYC

    Guest

    On 6-Oct-2003, Ben Pfaff <> 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?
    , Oct 6, 2003
    #14
  15. kelvSYC

    Micah Cowan Guest

    [OT] Re: Function Overloading

    "" <> writes:

    > On 6-Oct-2003, "Mike Wahler" <> 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
    Micah Cowan, Oct 6, 2003
    #15
  16. kelvSYC

    Guest

    Re: [OT] Re: Function Overloading

    On 6-Oct-2003, Micah Cowan <> wrote:

    > "" <> writes:
    >
    > > On 6-Oct-2003, "Mike Wahler" <> 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.


    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? :)
    , Oct 6, 2003
    #16
  17. Re: [OT] Re: Function Overloading

    lid <> scribbled the following:
    > On 6-Oct-2003, Micah Cowan <> 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 () ---------------------------\
    | 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
    Joona I Palaste, Oct 6, 2003
    #17
  18. kelvSYC

    Ben Pfaff Guest

    "" <> writes:

    > On 6-Oct-2003, Ben Pfaff <> 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \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\
    );}return 0;}
    Ben Pfaff, Oct 6, 2003
    #18
  19. Ben Pfaff <> scribbled the following:
    > "" <> writes:
    >> On 6-Oct-2003, Ben Pfaff <> 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 () ---------------------------\
    | 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
    Joona I Palaste, Oct 7, 2003
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,403
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    447
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    425
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    575
    John J. Lee
    Sep 30, 2005
  5. Fredrik Lundh
    Replies:
    0
    Views:
    395
    Fredrik Lundh
    Sep 30, 2005
Loading...

Share This Page