Is it appropriate to say that a function not having a return statement is valid?

Recommended Answers

All 5 Replies

Very conveniently. A function that doesn’t return anything returns a None object. In python, Not necessarily does the return keyword mark the end of a function it merely ends it when present in the function. Normally, a block of code marks a function and where it ends, the function body ends.

I could think of valid functions that perform an action, like formated printing. No need to return anything.

My background is in Smalltalk, so I can't understand the design decision to return None/void from a method/function that has no specific return value.
In Smalltalk a method with no explicit return wil always return the receiver object, which enables you to chain calls together like (pseudocode)
person.setName(etc).setAddress(etc).setDOB(etc).print()
Interestingly, Java under Oracle's management has recognised this and the latest APIs tend to return this; so calls can be chanied. It's called a "fluent interface" after Martin Fowler (2005)

Is this because Smalltalk is totally Object Oriented?
With Python you can use a number of different programming styles.

Yes, Smalltalk is totally OO, all it has are objects and messages. Blocks of code are just objects. The language doesn't even have loops or if tests - the same functionality is built with messages to Boolean objects, eg (pseudocode)
aBoolean.ifTrue(some code).ifFalse(some code) or aBoolean.whileTrue(some code)

But I think thats a different point from the default return value in Java etc. Returning this/self makes chaining calls easier and costs nothing. As far as I can see it's not too late - you could change Java to return this from void methods and it wouldn't break any existng code.

commented: In Python an object may be used in a boolean expression, e.g. if object_value: +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.