Same function different parameter var types

This is a discussion on Same function different parameter var types within the C Programming forums, part of the General Programming Boards category; Hello Cprogramming, I've got some code which I want to run for floats and doubles. Now I can make two ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    Same function different parameter var types

    Hello Cprogramming,

    I've got some code which I want to run for floats and doubles. Now I can make two functions like this:
    Code:
    void some_func_f(float f) { /* code */ }
    void some_func_d(double d) { /* same code */ }
    but then I got duplication of code. Is there any nice way to get this done in C without duplication of code?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Floats will get automatically promoted to doubles, assuming you mean what you say about "same code", so just do the second one.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Nope, I want the function to use 'real' floats.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rikvdk View Post
    Hello Cprogramming,

    I've got some code which I want to run for floats and doubles. Now I can make two functions like this:
    Code:
    void some_func_f(float f) { /* code */ }
    void some_func_d(double d) { /* same code */ }
    but then I got duplication of code. Is there any nice way to get this done in C without duplication of code?
    C does not permit function overloading... you need C++ for that.

    However in this case, with it returning void, you can use the second one for both because a double can hold a float value easily enough. Although, if you were returning a double, you might have a problem assigning it to floats.

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    for that you need templates..which means C++
    You ended that sentence with a preposition...Bastard!

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Not templates, overloading. But yes, C++, for now. If only you had a C1X compiler with Generic_ support...

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    why not templates? :O
    templates is like a super function overloading..and as you can he even has different function types..
    and that looks like templates to the rescue to me
    anyways...welcome back CTater
    Last edited by Eman; 02-07-2011 at 04:40 PM.
    You ended that sentence with a preposition...Bastard!

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Eman View Post
    why not templates? :O
    templates is like a super function overloading..and as you can he even has different function types..
    Actually templates use overloading... but function overloading is a native part of C++ that can be used at any level of the language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parameter names (without types) in function declaration
    By brightmatter in forum C Programming
    Replies: 1
    Last Post: 04-15-2010, 07:09 PM
  2. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 12:56 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21