Generic function in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anoopsr
    New Member
    • Mar 2008
    • 1

    Generic function in C

    hI,
    IHave a doubt in writing a generic function in C.

    Say for Example,

    I need to add 2 numbers. 2 interger Numbers or 2 float Numbers using the same function add().
    How do i call the add function i.e how do i send parameters.
    How do i receive the parameters in add fucntion definition.

    #include<stdio. h>

    int main()
    {
    int choice;
    int i1,i2;
    float f1,f2;
    printf("What type of number do u want to add?\n");
    printf("1.Inter ger Nmbers\n2.Float Numbers\n Enter Choice:");
    scanf("%d"&choi ce);

    switch(choice)
    {
    case1:printf("A dd Interger Numbers:");
    scanf("%d"&i1);
    scanf("%d"&i2);
    /*call to a generic function add() */
    /* HOW DO I SEND PARAMETERS IN THE FUNCTION CALL */
    add( ???);
    break;

    case 2: printf("Add Float Numbers:");
    scanf("%d"&f1);
    scanf("%d"&f2);
    /*call to a generic function add() */
    /* HOW DO I SEND PARAMETERS IN THE FUNCTION CALL */
    add( ???);
    break;

    }
    }

    /* HOW DO I RECEIVE THE PARAMETERS IN THE GENERIC FUNCTION ADD FOR BOTH FLOAT AND INT VALUES */

    add(????)
    {

    ...............
    ..............


    }


    Please Help. Its urgent.
    Thanks in advance.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    You can achieve the add function to add two numbers by writing a #define for the same.
    Try to work on that approach and ifu need help post again
    raghuram

    Comment

    • ashitpro
      Recognized Expert Contributor
      • Aug 2007
      • 542

      #3
      Originally posted by anoopsr
      hI,
      IHave a doubt in writing a generic function in C.

      Say for Example,

      I need to add 2 numbers. 2 interger Numbers or 2 float Numbers using the same function add().
      How do i call the add function i.e how do i send parameters.
      How do i receive the parameters in add fucntion definition.

      #include<stdio. h>

      int main()
      {
      int choice;
      int i1,i2;
      float f1,f2;
      printf("What type of number do u want to add?\n");
      printf("1.Inter ger Nmbers\n2.Float Numbers\n Enter Choice:");
      scanf("%d"&choi ce);

      switch(choice)
      {
      case1:printf("A dd Interger Numbers:");
      scanf("%d"&i1);
      scanf("%d"&i2);
      /*call to a generic function add() */
      /* HOW DO I SEND PARAMETERS IN THE FUNCTION CALL */
      add( ???);
      break;

      case 2: printf("Add Float Numbers:");
      scanf("%d"&f1);
      scanf("%d"&f2);
      /*call to a generic function add() */
      /* HOW DO I SEND PARAMETERS IN THE FUNCTION CALL */
      add( ???);
      break;

      }
      }

      /* HOW DO I RECEIVE THE PARAMETERS IN THE GENERIC FUNCTION ADD FOR BOTH FLOAT AND INT VALUES */

      add(????)
      {

      ...............
      ..............


      }


      Please Help. Its urgent.
      Thanks in advance.

      Use variable argument list
      check the following code.
      Code:
      #include<cstdarg>
      #include<iostream>
      #include<string>
      
      using namespace std;
      
      void add(char ch,...)
      {
      <spoonfed code removed-weaknessforcats>

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        If as you indicate, you can only have one add function to service ints and floats you need to write an add() function using T (template) principles so that the compiler will write the relevant function specifics appropriate to ints or floats when they are identified for processing.
        Ask yourself what does an add function do? A: it adds 2 int types or 2 float types. So this info should appear within the function parentheses. It is best to declare this function first before providing its implementation detail else where.

        Comment

        Working...