Thread: Function variables

  1. #1
    Registered User
    Join Date
    Feb 2010

    Function variables

    I'm just learning C and I had a general question. Is it improper (bad form?) to use the same variable names in a calling function and a called function? Here's a quick example of what I mean:

    main (calling function)
    int main (void)
       int fahrenheitTemp = 0;
       float celsiusTemp = 0.00;
       float kelvinTemp = 0.00;;
       int feetDistance = 0;
       float metersDistance = 0.00;
       int inchesDistance = 0;
       int poundsWeight = 0;
       float kilogramsWeight = 0.00;
       // calculate conversions using formulas found on the internet
       calc(fahrenheitTemp, feetDistance, poundsWeight, &celsiusTemp, &kelvinTemp, &metersDistance, &inchesDistance, &kilogramsWeight);
    } //end main
    calc (called function):
    void calc(int fahrenheitTemp, int feetDistance, int poundsWeight, float* celsiusTemp, float* kelvinTemp, float* metersDistance, int* inchesDistance, float* kilogramsWeight)
       calcTemp(fahrenheitTemp, celsiusTemp, kelvinTemp);
       calcDistance(feetDistance, metersDistance, inchesDistance);
       *kilogramsWeight = calcWeight(poundsWeight);
    Example: Should fahrenheitTemp have a different name in the calc() function than it does in main()? In a way, keeping them the same helps maintain readability for me, but it's also a bit deceiving since they are really two different variables.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    I've never heard anyone else's opinion on that, but I think it depends on the situation:

    - When you're passing pointers to structure, having the same name would help you remember that you really are pointing to the same object
    - Wen you're passing primitive types, having the same name could be confusing, because changes you make to one aren't reflected in the other

    I sometimes do it solely because there's really only one good name for the variable, and that's really the most important part. Parameters in functions should be named according to how they're used in that function, and sometimes there's really only one good, succinct and consistent way to describe the purpose of a variable.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Liverpool UK
    I think this is a two edged one, because i agree it is best to name the vars appropriate to their usage in the function, so i might have a function call with mouseX, mouseY as integers, then in the implementation i might rename them eg spriteX, spriteY, the one is a generic name that shows the vars to describe device position, the other is more specific to show the intended use in function to set a sprites movement or action or whatever.
    i think this is an acceptable way to do things but i also think that while i was starting out this used to confuse me sometimes when reading others code, whereas if they were named the same then it was plain what is being passed around.
    But then naming them the same as already mentioned may lead to confusion about the 'permanence' of the vars and changes made to them.
    with pointers to structures i use the convention of same name with prefix 'p',
    as an aside i have often read about naming conventions such as always putting a small 'i' before an integer name, 'p' with pointer etc, but never really seen it, is this an outdated thing??
    Last edited by rogster001; 02-16-2010 at 03:55 AM.

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Yes rogster, prefixing a variable name with information about its type is called Hungarian Notation. For the most part it is considered poor practice nowadays.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying Not To Use Global Variables With Function
    By Krash005 in forum C Programming
    Replies: 8
    Last Post: 12-04-2009, 01:28 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM