Thread: Functions with multiple definitions

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    USA
    Posts
    15

    Question Functions with multiple definitions

    Hello All,

    I'm really rusty in C/C++ and was wondering how you declare a function that can take multiple data types as parameters. For instance, lets say you were making your own print function that could accept a string or an integer as an input parameter. I remember doing this in a programming class a while back but there was some way that you do the function definition so that you make two different functions, one to handle the string input and another one to handle the integer but they both have the same name.... What is that called again and is that used in C or only C++?

    Thanks,
    Jason O

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It's called a template -- you can also google "generic programming", and it is only available in C++.

    Depending on exactly what you want to do, you may probably be able to do it in C using void pointers and an extra param to indicate type.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Well, no. The actual mechanism is function overloading. Declare and define two functions with the same name but different argument types.

    Template functions provide one method of achieving function overloading, in the case where the definition of the function is applicable to multiple types.

    Both templates and function overloading are C++ specific.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Sorry, grumpy is correct. The difference between using a template and overloading a function is that without a template, the function must be defined multiple times. With a template, you can write one single function definition to cover multiple cases.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jun 2008
    Location
    USA
    Posts
    15
    Ahhh ok! Overloading was the term I was looking for. Thank you!!

    - Jason O

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  2. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  3. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  4. Replies: 4
    Last Post: 03-11-2005, 05:45 PM
  5. multiple definitions of msg
    By laasunde in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2002, 09:31 PM