CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Very common mistakes with parameter passing, continued

  • Another common mistake is to assume that function arguments and parameters must have the same names to work correctly.

    Some novice programmers think that the matching of names is how the arguments are evaluated, and how arguments are bound to parameters.
    For example, consider the code:

    
    int sum3( int a, int b, int c )
    {
        return a + b + c;
    }
    
    ....
    
        int a, b, c;
    
        a = 1;
        b = 4;
        c = 9;
        printf("%i\n", sum3(c, a, b) );
    

    Here, the arguments are not "shuffled" until the names match.

    It is not the case that arguments must have the same names as the parameters they are bound to.
    Similarly, the names of variables passed as arguments are not used to "match" arguments to parameters.

    If you ever get confused by this, remember that arithmetic expressions, such as 2*3 + 1, do not have names, and yet they are still valid arguments to functions.

 


CITS2002 Systems Programming, Lecture 4, p8, 31st July 2024.