LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-16-2008, 10:53 AM   #1
raskol
Member
 
Registered: Mar 2008
Posts: 51

Rep: Reputation: 15
Function design, 1 return-statement?


btw noob-question, is return considered a statement?

is it good design to have only one return per feunction if possible or it doesnt matter?

which is better:

Code:
def power(nbr, power):
    if power >= 1:
        acc=nbr
        for x in range(1, power):
            acc=acc*nbr
        return acc
    if power == 0:
        return 1
    if power < 0:
        acc=1
        x=power/-1
        while x > 0:
            x=x-1
            acc=acc/nbr
        return acc
or

Code:
def power2(nbr, power):
    acc=1
    if power >= 1:
        acc=nbr
        for x in range(1, power):
            acc=acc*nbr
    if power < 0:
        acc=1
        x=power/-1
        while x > 0:
            x=x-1
            acc=acc/nbr
    return acc
the second one is shorter obv but if u could construct a scenario where it is the same anyhow, better to have only 1 return?
 
Old 04-16-2008, 11:10 AM   #2
raskol
Member
 
Registered: Mar 2008
Posts: 51

Original Poster
Rep: Reputation: 15
Code:
def power3(nbr, power):
    acc=1
    if power >= 1:
        acc=nbr
        for x in range(1, power):
            acc=acc*nbr
    if power < 0:
        if nbr!=0:
            acc=1
            x=power/-1
            while x > 0:
                x=x-1
                acc=acc/nbr
        else:
            return "Division by zero"        
    return acc
or

Code:
def power(nbr, power):
    if power >= 1:
        acc=nbr
        for x in range(1, power):
            acc=acc*nbr
        return acc
    if power == 0:
        return 1
    if power < 0:
        if nbr!=0:
            acc=1
            x=power/-1
            while x > 0:
                x=x-1
                acc=acc/nbr
            return acc
        else:
            return "Division by zero"

any idea how to use only 1 return statement in power3?
 
Old 04-16-2008, 05:56 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
There is no correct answer to this. However there is a school of thought that it is good practice to minimise the points of exit from a function. Thus power() has three exit points whilst power2() has just the one, so power2() is thought by some to be "cleaner".

The problem arises with the example you provided with power3() where you are returning different data types. This is solved in strong typing languages by the use of exceptions. A construct that has been adopted by many weak typing languages as well. It's advantage is that in normal running you know what will be returned (and in the case of power2() where), however when something unexpected occurs, for example division by zero, then you immediately jump out of the function and let the exception handler take control.
 
Old 04-16-2008, 08:24 PM   #4
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
Schools of thought have no real place in practice. Use the style that makes the code cleaner to read. If its more natural to have 1 return, have 1 return. If its more natural to have 100,000,000,000 returns, you have no clue how to design code. If its more natural to use 10 returns, use 10 returns.

Such schools of thought like "thats minimize this" or "don't use that" are based solely on theory, and in practice, usually makes code harder to read because you have to bang a function into shape using to use a un-natural methodology.
 
Old 04-16-2008, 10:00 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by SciYro View Post
Schools of thought have no real place in practice.
On the contrary where would we be without schools of thought, goto ghetto most likely?


The "theory" behind minimising the number of returns revolves around trying to make it easier to understand the flow of control in a large program. As you suggested excessive number of returns is most likely the result of poor design. Since the same school of thought suggests that functions should be small compact beasts that perform a single task it is normally easy to restrict the number of returns when following that ideology. But as I said there is no correct answer. There are of course opinions aplenty
 
Old 04-16-2008, 11:55 PM   #6
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
One thing that is definitely bad style, however, is this:

Code:
def power(nbr, power):
It's not a good idea to give a function argument the same name as the function. Especially if you want to make recursive calls.
 
Old 04-17-2008, 12:02 AM   #7
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
One approach that follows the letter, although not necessarily the spirit, of having only one return statement is:

Code:
class Return(Exception):
    pass

def power(nbr, exponent):
    try:
        if exponent >= 1:
            acc = nbr
            for x in range(1, exponent):
                acc = acc * nbr
            raise Return()
        if exponent == 0:
            acc = 1
            raise Return()
        if exponent < 0:
            acc = 1
            x = exponent / -1
            while x > 0:
                x = x - 1
                acc = acc / nbr
            raise Return()
    except Return:
        return acc
 
Old 04-17-2008, 12:08 AM   #8
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
A more elegant solution:

Code:
from __future__ import division

def power(base, exponent):
    """Equivalent to base ** exponent where exponent is an integer."""
    neg_exp = exponent < 0
    if neg_exp:
        exponent = -exponent
    result = 1
    for index in xrange(exponent):
        result *= base
    if neg_exp:
        result = 1 / result
    return result
Note that by starting the loop from 0 instead of 1, x**0 doesn't need to be a special case. Also, rather than have two separate loops, just use the identity x**(-y) == 1/x**y.
 
Old 04-17-2008, 07:24 AM   #9
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
It is not the number of returns in a function. It is how there are placed and used.

It is said (again such a rule of thumb) that a function should not be longer that the size of a piece of paper, that is about 60 lines. Now since we never use paper anymore (don't we?) it is easier to have a function grow without noticing. My functions are usually anything between 10 and 200 lines.

Now in such a 100+ lines function, I consider it bad to hide some returns randomly and deeply embedded in the algorithm. In that case it is (a) hard to follow the program flow (b) difficult to see what value is returned in that stage of the program.

On the other hand, to perform a number of tests or simply calculations at the start of the function, which all result in a return doesn't matter at all, even if you have 10 of such cases.

Neither should it be a problem if at the entrance of a function the program flow splits into a number of blocks (with a case statement for example) and each block is exited directly with a return.

On the the contrary, such a construct might be much better than a design following the text book and create an artificial single exit point with complicated if-then-else constructs.

Tell you something. When I write functions with multiple decision points where I have to exit, but all exits require the same piece of common code (like releasing a semaphore), I use a GOTO to get there. Yes! Code unreadable? NO! Not as long as your goal is to design readable code.

jlinkels
 
Old 04-17-2008, 07:55 AM   #10
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
From the prospective of a guy that largely does C and C++ development, it is usually resource management that causes me to always use only 1 return from a function. If I had a nickel for every memory leak I have fixed because somebody allocates memory and returns in a conditional which doesn't free said memory I'd be a very rich man. With functions that have to do several allocations you end up having to do several frees with your return so it quickly becomes annoying to do anything other then a single point of return. Because I'm so conscious of this I find pretty much every function I design ends up with a single point of return. This, of course, is not really an issue in languages that are garbage collected.
 
Old 04-17-2008, 09:52 AM   #11
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
jtshaw:

That was the other reason for that common piece of code I mentioned to be executed before a return. How could I ever forget to mention freeing memory?

jlinkels
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how return statement works venmugil Programming 3 02-28-2007 11:10 AM
java +missing return statement? no it isnt trscookie Programming 9 04-21-2006 08:57 PM
segmentation fault on return statement crosseyedalien Programming 9 06-17-2005 12:23 PM
return statement in functions pantera Programming 2 12-06-2004 06:21 PM
value is lost from a return statement! what?? ludeKing Programming 3 05-30-2004 08:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration