[Tutor] function return values

Cameron Simpson cs at cskk.id.au
Fri Dec 3 02:10:13 EST 2021


On 03Dec2021 16:57, Phil <phillor9 at gmail.com> wrote:
>This was originally isBoxPairsInSameRow, I just dropped the "is" (Pairs 
>should have been Pair) since I'm not returning a boolean value.  Coming 
>up with descriptive names  for functions and variable is not one of my 
>strong points and I must check if camel text is acceptable in Python.

Functions starting with "is" usually return a Boolean because they 
usually test something ("is this blue?" etc). If your function actually 
returns the pair found (with some context information) you'd give it a 
nounlike name, naming the thing it returns somehow.

    def is_odd(n):
        return n % 2 != 0

    def square(n):
        return n * n

    # print the squares of the odd numbers < 100
    for n in range(100):
        if is_odd(n):
            print(square(n))

Anyway, it's your code. Do what you find easiest to read later. Having 
things sort-of-like English grammar helps me though. And don't stress 
about names too much. If you find a name confusing or not to your liking 
it is easy to fix with an editor later.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list