2 Replies - 507 Views - Last Post: 08 August 2014 - 02:18 PM Rate Topic: -----

#1 jmoore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-August 14

Naming function parameters

Posted 08 August 2014 - 01:47 PM

When naming function parameters I always feel I should ensure that they do not share the same name as any variables I am passing in as arguments. I think this is for two reasons 1) I recall my engineering tutor telling me this was bad practice (long, long ago using pascal) 2) I'm a teacher and I feel students may infer that it is in fact necessary that they share the same name if I use examples where this occurs. So for example, if I have a function that has height and width parameters, if I call them height and width, I will typically end up using the same names for variables in the arguments when calling that function eg print("area is = " + rect_area(height,width)). Aside from the fact using very common names like height and width in the first place may not a great idea, should I be concerned?

Thanks in advance


Is This A Good Question/Topic? 0
  • +

Replies To: Naming function parameters

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,471
  • Joined: 12-December 12

Re: Naming function parameters

Posted 08 August 2014 - 01:54 PM

You shouldn't be concerned.

I also think it is useful in the first few examples to NOT use the same names for arguments and parameters, so that students don't get hold of the idea that they have to be the same. Once passed this hurdle though, there is no reason why they cannot be the same, and they often are. This also introduces another important topic of scope.

This post has been edited by andrewsw: 08 August 2014 - 01:55 PM

Was This Post Helpful? 2
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon

Reputation: 11021
  • View blog
  • Posts: 18,804
  • Joined: 19-March 11

Re: Naming function parameters

Posted 08 August 2014 - 02:18 PM

You should focus on understanding and clarity. For understanding, make sure that the students grasp the idea that a parameter in a function definition declares a new local variable (if not in those words yet, they should certainly understand what it means). For clarity, once they understand what the parameters are and what they mean, you should encourage them to use the same name if it makes the code easier to read. You should certainly not give them the idea that they have to come up with new synonyms for "height" and "width" or otherwise mangle the perfectly clear names!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1