Friday, October 2, 2009

More about Function Definitions, Parameters, etc

Rather than
def cat_twice(part1, part2):
cat = part1 + part2
print_twice(cat)
It makes more sense, to me, to write
def y_twice(a, b):
y = a + b
print_twice(y)

This defines y as a + b, because it is the function is indented it's a local variable and only works within y_twice. Parameters also only work within their set function. Because y is defined as a + b, print_twice(y) is the same as print_twice(a + b). This could make writing code much more efficient.


1 comment:

  1. The issue of how best to name variables will be a recurring theme. "cat" was meant to be short for "concatenate", and thus to indicate that this function is intended for use with strings, not numbers. In that context, "part1" and "part2" are better than "a" and "b".

    The use of "cat" for "concatenate" is odd, I realize, but it makes sense to someone familiar with the Unix operating system.

    ReplyDelete