2

I'm new to this, hope someone can help with my basic question to help clarify my concept.

I was playing around to shorten the code, but the 2nd case didn't work.

I think it didn't work because remove() doesn't return anything. Is that correct?

This got me wondering. When do you want write function that doesn't return anything? Isn't it always good to return something even though it might be get used? How do you work with function that doesn't return anything.

s = 'abc'

# This remove a from the abc
x = list(s)
x.remove('a')

# if remove() can return the list this would be simpler
x = list(s).remove('a')
vagavince
  • 37
  • 1
  • 5

4 Answers4

4

Many programmers are proponents of what is known as the Command-query separation. Functions that return values (queries) will in general not have side effects, while functions that have side effects are commands that in general won't return much interesting data. The separation has the intention of making it easier to keep track of changes in global state.

Henrik
  • 4,254
  • 15
  • 28
2

This is a matter of style. I, and plenty of others, agree that in general it is much more useful to have a function return something - in particular, methods of an object which return self rather than None allow chaining.

Unfortunately, that's not what most people in the Python community do, and it's not what the creators of the built-in libraries do. You're going to have to learn to cope with this, and given that the style is pervasive, decide whether you want to cope with switching between styles, or conform to the dominant paradigm.

Marcin
  • 48,559
  • 18
  • 128
  • 201
2

A function always returns something: if there is no return excuted during the call of a function , None is returned.

It may happen that no return is excuted during a call, when none of the conditions that lead to a return gives a True result, as in the following code:

def f(x):
    if x==2:
        return 'hello'

y = f('brouef')

print 'y ==',y

it prints

y == None

That also may happen if there is no return statement in the code block of the function, as in the following code.

.

That said, an algorithm doesn't always require to use returns of function. A function can transform something without to return anything else than a None.
For example:

li = [1000]

def f(x):
    for i in xrange(100):
        if i%3==0 and not i in range(20,85):
            li.append(i)

print li
f(li)
print li

result

[1000]
[1000, 0, 3, 6, 9, 12, 15, 18, 87, 90, 93, 96, 99]

That's the choice that was taken for sort(), for example, to make the coder well aware that the sorted object is changed in place. That is said in the doc.

eyquem
  • 26,771
  • 7
  • 38
  • 46
0

It makes sense if the purpose of the function is to issue a side effect, like changing a global variable, performing input/output or call other functions with side effects. And in the end "no value" is a return value in itself, which is why e.g. functions in the C programming language are tagged with return type void.

Functional programming languages see side effects as a bad thing, and limit them.

mvw
  • 5,075
  • 1
  • 28
  • 34