Thread: how to add a parameter to the function

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    16

    how to add a parameter to the function

    How do I add one more argument to an existing function which is called by other functions from various modules of the project?
    I would like to add this new argument without or minimum risk. Is there a way to avoid compilation of the modules from which it is called?
    (There is a separate header file containing the definition of the function)


    Thanks in advance.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    no you cannot do it, you should also pass the additional parameter...

    What you can do - to write a function with another name and additional parameter
    and replace the body of the old function with the call to the new one

    Then all calls to the old one can be leaved untoched, and new places will call the new function

    Code:
    int doWork(int a, int b) //old function
    {
      return doWorkNew(a,b,10 /*default value for the new paraameter*/);
    }
    
    int doWorkNew(int a, int b, int c)
    {
    //copy here the body of the old doWork function and modify it to process the new parameter
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    Asume that you define function like this:
    Code:
    int foo(void *);
    than you can pass to function different number of arguments. Okay its only one real argument but void * can point to array of arguments.
    Code:
    #include <stdio.h>
    
    int foo(void *);
    
    int main()
    {
       char arg1[] = {2, 1, 2};
       char arg2[] = {3, 1, 2, 3};
    
       printf("%d\n", foo((void *) arg1));
       printf("%d\n", foo((void *) arg2));
      
      return 0;
    }
    
    int foo(void * arg)
    {
       int acc=0, i;
       char * tmp = arg;
       
    /* first element of array is num of arguments */   
       for (i=0; i<tmp[0]; i++)
       {
          acc += tmp[i+1];
       }
       
       return acc;
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by andor
    Asume that you define function like this:
    Code:
    int foo(void *);
    than you can pass to function different number of arguments. Okay its only one real argument but void * can point to array of arguments.
    Code:
    #include <stdio.h>
    
    int foo(void *);
    
    int main()
    {
       char arg1[] = {2, 1, 2};
       char arg2[] = {3, 1, 2, 3};
    
       printf("%d\n", foo((void *) arg1));
       printf("%d\n", foo((void *) arg2));
      
      return 0;
    }
    
    int foo(void * arg)
    {
       int acc=0, i;
       char * tmp = arg;
       
    /* first element of array is num of arguments */   
       for (i=0; i<tmp[0]; i++)
       {
          acc += tmp[i+1];
       }
       
       return acc;
    }
    int this case better to pass the char* pointer without cast
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No you cannot pass a different number of arguments. You're stil passing one argument. You're passing an array. That's one argument. Void pointers don't help you get around that.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    16
    Thanks a lot for your suggestions vart ,andor & quzah !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  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. Replies: 3
    Last Post: 03-04-2005, 02:46 PM