Convention for naming function parameters

How do you prefer to name function parameters, so that they are not confused with the variables in the calling function?

Consider this simple example:

   int fnc(int val) { return ++val; }
   val = fnc(val);

Two ways of handling this are:

   int fnc1(int aVal) { return ++aVal; }
   val = fnc1(val);

   int fnc2(int _val) { return ++_val; }
   val = fnc2(val);

But I would think there are better conventions.

I usually just use more descriptive variable names in the calling function. And more descriptive function names.

Instead of:

  int fnc1(int time) { return ++time };
  time = fnc1(time);

Maybe use:
  int updateTime(int time) { return ++time };
  updatedTime = updateTime(currentTime);

why would they be confused?

Maybe only I would be confused, J-M-L. Hahaha

I'm thinking of a scenario where two different calls are made to the same function. Consider this simple example:

int fnc(int val) { return ++val; }
val = fnc(val);
different_val = fnc(different_val);

This smells of inconsistency to me. Also, the scope of "val" is ambiguous.

I prefer Jimmus' approach. In the function, he used a parameter name of a more generic item than the item being passed from the calling function. That is:

"currentTime" is a type of "time"

int updateTime(int time) { return ++time; }
updatedTime = updateTime(currentTime);
laterTime = updateTime(currentTime);

This approach could get messy, though, if care is not taken to use consistent names. For example:

int updateTime(int velocity) { return ++velocity; }
updatedTime = updateTime(currentTime);
laterTime = updateTime(currentTime);

Another approach I have seen is to use "this_" in the function. For example:

int fnc(int this_val, int this_offset ) { return this_val + this_offset; }
val = fnc(val, offset);

This is reminiscent of C++, but seems clumsy to me.

the scope of "val" is ambiguous.

No it's not. The val variable in the function is not the same as the val variable assigned by calling the function which must have been declared elsewhere. They each have their own scope. What is confusing is the use of 2 different variables with the same name. This should be familiar to you from your other thread where you had this problem.

Give functions names which describe what they do.

Give variables and constants names which describe what they contain.

So func() is a silly name for an increment function. Call it increment() or incrementByOne(). Yes, I know you're only using increment as an example but that is what this function does so that is what its name must be.

print() always prints. There's lots of things it can print: strings, numbers, characters... There's lots of places it can print to, it may be Serial3.print() or LCD.print(). But its name tells you what it does.

Yeah - only confusing if you are confused about scope and proper naming :slight_smile:

My mind hurts.. I think I will return to the refreshment of my avatar. hahaha.

The upshot is, names have consequences.