Functions returning arbitrary type

Discussion in 'C Programming' started by Andrej Prsa, Jul 6, 2003.

  1. Andrej Prsa

    Andrej Prsa Guest

    Hello, List!

    Does ANSI C support in any way functions that return different types
    depending on the given arguments? I.e.

    int my_function (int arg)
    double my_function (double arg)
    char *my_function (char *arg)

    to be joined in a single my_function somehow? I tried playing with void*,
    but I didn't get far really.

    Help?

    Thanks,

    Andrej
    Andrej Prsa, Jul 6, 2003
    #1
    1. Advertising

  2. Andrej Prsa

    Kevin Easton Guest

    Andrej Prsa <> wrote:
    > Hello, List!
    >
    > Does ANSI C support in any way functions that return different types
    > depending on the given arguments? I.e.
    >
    > int my_function (int arg)
    > double my_function (double arg)
    > char *my_function (char *arg)
    >
    > to be joined in a single my_function somehow? I tried playing with void*,
    > but I didn't get far really.
    >
    > Help?


    No. The closest you can get is using a union:

    union foo {
    int i;
    double d;
    char *cp;
    };

    ....but you'll still need to tell your function what kind of type you
    actually passed it in the union, so the function will need code
    equivalent to:

    switch (type_used) {
    case TYPE_INT:
    /* retrieve int argument */
    break;
    case TYPE_DOUBLE:
    /* retrieve double argument */
    break;
    case TYPE_CHAR_P:
    /* retrieve char * argument */
    break;
    default:
    }

    /* Common code */

    switch (type_used) {
    case TYPE_INT:
    /* return int argument */
    /* ... */

    So you might as well just have a function like:

    int my_func_int(int n)
    {
    /* convert int to type common code expects */
    y = my_func_common(x);
    /* convert y to int and return it */
    }

    ...and similar for the double and char * cases - this lets you factor out
    the common case.

    - Kevin.
    Kevin Easton, Jul 6, 2003
    #2
    1. Advertising

  3. Andrej Prsa

    Joe Wright Guest

    Andrej Prsa wrote:
    >
    > Hello, List!
    >
    > Does ANSI C support in any way functions that return different types
    > depending on the given arguments? I.e.
    >
    > int my_function (int arg)
    > double my_function (double arg)
    > char *my_function (char *arg)
    >
    > to be joined in a single my_function somehow? I tried playing with void*,
    > but I didn't get far really.
    >
    > Help?
    >

    We are not a List, thank you. Your question describes one of the many
    ways C++ differs from C, function overloading. I think you must already
    know this. Any book on C++ explains it and no book on C mentions it.

    The short answer is, of course, no.
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Jul 6, 2003
    #3
  4. Andrej Prsa

    Malcolm Guest

    "Joe Wright" <> wrote in message
    >
    > We are not a List, thank you. Your question describes one of the many
    > ways C++ differs from C, function overloading. I think you must already
    > know this. Any book on C++ explains it and no book on C mentions it.
    >

    If the OP learned C++ before C, why would he know that this feature is not
    available in C?
    Malcolm, Jul 6, 2003
    #4
  5. Andrej Prsa

    Joe Wright Guest

    Malcolm wrote:
    >
    > "Joe Wright" <> wrote in message
    > >
    > > We are not a List, thank you. Your question describes one of the many
    > > ways C++ differs from C, function overloading. I think you must already
    > > know this. Any book on C++ explains it and no book on C mentions it.
    > >

    > If the OP learned C++ before C, why would he know that this feature is not
    > available in C?


    It's my sense of smell. My own minor C++ book explains overloading,
    mangling, etc. in C++ and explains that C can't do it. A C++ weenie
    would know that much. I smell troll.

    But hey, what do I know? There's pollen, dander..
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Jul 6, 2003
    #5
  6. On Sun, 6 Jul 2003 01:07:17 +0200, Andrej Prsa
    <> wrote:

    > Hello, List!
    >
    > Does ANSI C support in any way functions that return different types
    > depending on the given arguments? I.e.
    >
    > int my_function (int arg)
    > double my_function (double arg)
    > char *my_function (char *arg)
    >

    C99 extends several of the standard-library math functions in
    <tgmath.h> to be overloaded on the three standard floating types.
    There is (and was) no requirement for a similar facility for user
    functions, although some implementations may do so anyway.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Jul 15, 2003
    #6
    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. Now With More Salt

    Arbitrary Type Definition

    Now With More Salt, Sep 17, 2003, in forum: C++
    Replies:
    4
    Views:
    1,353
    Thomas Matthews
    Sep 17, 2003
  2. Honestmath
    Replies:
    5
    Views:
    554
    Honestmath
    Dec 13, 2004
  3. Steven D'Aprano

    Timing out arbitrary functions

    Steven D'Aprano, Dec 24, 2005, in forum: Python
    Replies:
    5
    Views:
    299
    antti kervinen
    Dec 28, 2005
  4. eastern_strider

    member functions returning class type

    eastern_strider, Dec 27, 2007, in forum: C++
    Replies:
    7
    Views:
    309
    Jim Langston
    Dec 28, 2007
  5. Eric des Courtis
    Replies:
    10
    Views:
    1,610
    Lowell Gilbert
    Mar 11, 2008
Loading...

Share This Page