#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    56
    Rep Power
    0

    Why we do like this


    Code:
    #include<iostream>
    float zarbing(float, float);
    using namespace std;
    int main()
    {
      float x =0,y =0,z =0;  
       cout<<"Enter a value for which u want multiplication : "; 
       cin>>x;
       cout<<"Enter 2nd value with which u want to multiply : "; 
       cin>>y;
       z = zarbing (x, y);
       cout<<"The answer after multiplacation is : "<< z<<endl;
       system("Pause");
       return 0;
    }
    
    //definition of multiplacation function :)
    
    float zarbing(float a, float b) //Why use a , b?Why not x,y?
    {
          float t = 0;
          t = a*b;
          return (t);
    }
    Notice in above function program there is a function in which i am string values in x,y & passing the values to a function zarbing. but @ the time of definition Why there is a, b along with data type. Why not i can write x, y with same data type. I even tried it with x, y & everything run smooth but till yet i seen many examples in books & on internet everyone use new names to store value of x, y from above. Can someone tell me the reason of this thing?What would be error if we start using the same variable names in function as well. mean calling function & called function both have same variable names. I read in book @ some point where the auther was saying that we don't want our function to access our function directly in call by value situation.
  2. #2
  3. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1111
    There wouldn't be any errors, and you may use the same variable names. It's done to avoid any confusions to the readers, so that they understand that they are two different variables. Same name to the variables may confuse beginners more often than not.

    Comments on this post

    • mike65535 agrees : Yes, to show, through example, that the names don't matter.
  4. #3
  5. um, Hello?
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    FN23fc
    Posts
    719
    Rep Power
    165
    Moreover, the parameter names can even differ between the prototype and the function definition.

    Code:
    short increment_me( short a );
    
    short increment_me ( short x ) {
        x++;
        return x;
    }
  6. #4
  7. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1111
    Moreover, the parameter names can even differ between the prototype and the function definition.
    That's because the parameter names in the prototype are ignored altogether by the compiler.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,119
    Rep Power
    1807
    Consider the following situations and how you might distinguish between the different variables and function arguments.:

    • Calling the same function more than once with different parameters stored in different variables.
    • Calling different functions having the same argument names but passing different parameters in different variables to each.
    • Passing the same parameter variable to multiple functions with different argument names.
    • Calling library functions where you did not choose the argument names.

    They cannot all have the same names! There are numerous many-into-one and one-into many combinations.

    In most cases you would choose variable names that made sense in the application domain, and as such single letter variable names outside of perhaps purely arithmetic or maths functions do not represent good variable or argument names. They are often used in example code because often such examples are themselves meaningless just like the variable names (and your example). You should recognise such code as such and not treat the naming convention as an exemplar of good practice.

    Often, especially with maths functions, the names of arguments are conventional, for example it is normal to express Cartesian coordinates as x,y, so these would be acceptable in a generic Cartesian function, while in a calling context you might use more meaningful domain specific names such as player1_position_x, player1_position_y.

    So in short, it is impractical (and pointless) to match variable names to argument names, and in any case in the calling context, you should use variable names that make sense in that context, while in the function, the arguments, are often more generic and would be named accordingly.
    Last edited by clifford; April 1st, 2011 at 04:23 PM.
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,119
    Rep Power
    1807
    Originally Posted by RAJ_55555
    It's done to avoid any confusions to the readers, so that they understand that they are two different variables. Same name to the variables may confuse beginners more often than not.
    The more practical and obvious reason is that a single function may be called with many different variables as parameters. It would be silly to have to copy all your parameters to variables with matching names before calling a function!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    56
    Rep Power
    0
    Sir clifford ur explnation is always great. seems u have an excellent hold on this subject ;)

IMN logo majestic logo threadwatch logo seochat tools logo