Thread: regarding arguments

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    regarding arguments

    Hi,

    I have this clarification. if we are passing arguments to a function, is it better to work completely with the arguments or create a local copy of the argument and work with the copy of the argument.
    For example

    Code:
    int main(void)
    {
    uint8_T var=0;
    dummy_function (&var);
    }
    
    void dummy_function(uint8_T *a_var_u8)
    {
      uint8_T *p_localcopy_u8 = a_var_u8; 
    
    // use a_var_u8  or
    // use p_localcopy_u8  
    // for the remaining programming
    }
    Thanks
    satya

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    There is a difference between passing argument; and passing a argument by address/pointer!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Satya
    if we are passing arguments to a function, is it better to work completely with the arguments or create a local copy of the argument and work with the copy of the argument.
    The function's parameters are variables local to the function. Do you need a copy? If you don't, then why bother?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Quote Originally Posted by stahta01 View Post
    There is a difference between passing argument; and passing a argument by address/pointer!

    Tim S.
    Yes. But my question is whether i pass argument or argument by address/ pointer, at the function definition there is a variable inside the parenthesis can i use the name of the same variable or it is better to make a copy of the same and use the name of that variable?
    Code:
    function(int var)
    {
    printf("%d",var);
    }
    // is better or
    function (int var)
    {
    int temp=var;
    printf("%d",temp);
    }
    
    similarly for pointers
    
    void function(int *p)
    {
    int value = *p;
    printf("%d",value);
    }
    
    or
    
    void function(int *p)
    {
    int *temp = p;
    value = *temp;
    printf("%d",value);
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Satya
    Code:
    function(int var)
    {
        printf("%d",var);
    }
    // is better or
    function (int var)
    {
        int temp=var;
        printf("%d",temp);
    }
    If the latter is better than the former, then this is even better:
    Code:
    void function(int var)
    {
        int temp0 = var;
        int temp1 = temp0;
        int temp2 = temp1;
        int temp3 = temp2;
        int temp4 = temp3;
        int temp5 = temp4;
        int temp6 = temp5;
        int temp7 = temp6;
        int temp8 = temp7;
        int temp9 = temp8;
    
        printf("%d", temp9);
    }
    Quote Originally Posted by Satya
    Code:
    void function(int *p)
    {
        int value = *p;
        printf("%d", value);
    }
    In this particular trivial example, it would be better to just use *p. That said, sometimes it does make sense to copy what the pointer points to to a local object, e.g., you are dealing with a pointer to a pointer that is pretty much always dereferenced in the body of the function, so instead of writing:
    Code:
    void function(int **p)
    {
        *p = malloc(etc);
        /* use *p and (*p)[i] here and there */
    }
    you might write:
    Code:
    void function(int **p)
    {
        int *q = malloc(etc);
        /* use q and q[i] here and there */
        *p = q;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you're really worried about changing something when you shouldn't, declare it const

    void dummy_function(const uint8_T *a_var_u8)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Satya View Post
    if we are passing arguments to a function, is it better to work completely with the arguments or create a local copy of the argument and work with the copy of the argument.
    That depends on what your criterion is for "better". Better is not an absolute. Is a green car better than a blue one? Only if you like green cars. [Of course, red cars are always better, because they are faster].

    It is sometimes appropriate to copy arguments, depending on what the function is actually doing. To state the obvious, if the required effects can be achieved without creating a local copy, then it is not necessary to create a local copy. If the code is more understandable by working with a local copy, the there is nothing wrong with that.

    If the function needs to perform rollback (performs a lot of operations, with reversion to original state if any of those operations fail) then one way is to store original values, and write them back if needed. Alternatively, a copy might also be made, and all operations performed on the copy, which is only written to the argument at the end once all other operations have succeeded. Another approach is for the function might track completely different data about operations performed, so it can literally unwind the operations performed (in which case it won't store copies of the original arguments, but will store other data so it can do the rollback).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arguments in MFC
    By guitarist809 in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2008, 07:23 PM
  2. Arguments
    By Suudsu2200 in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2007, 03:10 PM
  3. passing arguments using "Command Line Arguments"
    By Madshan in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 03:46 PM
  4. Int Arguments
    By Scytz0 in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2003, 07:55 PM
  5. arguments
    By SMB3Master in forum C++ Programming
    Replies: 1
    Last Post: 09-04-2003, 08:31 PM