Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
January 29th, 2013, 12:29 AM
 chaying
Registered User

Join Date: Jan 2010
Posts: 9
Time spent in forums: 2 h 39 m 46 sec
Reputation Power: 0
Solved : Return statement

Hi

I got the following code which does what I want except one thing, if I do :

Code:
```r = dectohex(39784)
print r```

The result is None ??
It only works if I write print instead of return..

Code:
```# IMPORTS
import string

# list of HEX to DEC correspondances
correspondances = {10:'A', 11:'B', 12:'C', 13:'D', 14:'E', 15:'F'}

# list hosting translated digits
hnumb = []

# convert function dec to hex
def dectohex(x):
mod = x%16
if mod > 9:
hdigit = correspondances.get(mod)
else:
hdigit = mod

hnumb.append(hdigit)

if x/16 == 0:
result = ''.join(str(i) for i in hnumb)
return result[::-1]
else:
dectohex(x/16)```

Could someone explain to me why ?

Thank you

#2
January 29th, 2013, 02:10 AM
 SuperOscar
Contributing User

Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404
Time spent in forums: 1 Week 5 h 17 m 52 sec
Reputation Power: 65
Quote:
 Originally Posted by chaying Code: ``` if x/16 == 0: result = ''.join(str(i) for i in hnumb) return result[::-1] else: dectohex(x/16)``` Could someone explain to me why ?

Because your “else” branch lacks a “return”.

Your function is defective in many other ways as well. For example, since “hnumb” is not a local function, the result will be different if hnumb != [] to begin with. These kind of variables should all be local.
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

#3
January 29th, 2013, 02:29 AM
 chaying
Registered User

Join Date: Jan 2010
Posts: 9
Time spent in forums: 2 h 39 m 46 sec
Reputation Power: 0
Hi Oscar

Thanks to stop by.

Sorry but I don't undertstand : 'lack of a return statement' ? What do you mean ? If I put a return statement :
Code:
```else:
dectohex(x/16)
return```

Wouldn't that be some 'dead code' : this code will never be reached, will it ?
edit: after test, it doesn't change anything.

And I modified my function inserting a :
Code:
`global hnumb`

after the function definition but I don't see how it could be a local variable as it would be reinitialized each time, wouldn't it ?

You said 'many ways', what other ways would you think about ?

#4
January 29th, 2013, 03:57 AM
 SuperOscar
Contributing User

Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404
Time spent in forums: 1 Week 5 h 17 m 52 sec
Reputation Power: 65
Quote:
 Originally Posted by chaying Sorry but I don't undertstand : 'lack of a return statement' ? What do you mean ? If I put a return statement : Code: ```else: dectohex(x/16) return```

“return” without an argument is the same as “return None” which, incidentally, is also the default return value for a function without a return statement, so of course that doesn’t change anything.

But what do you expect the line previous to that does? Put a return before the “dectohex(x/16)” so the function might ultimately actually return something.

Quote:
 And I modified my function inserting a : Code: `global hnumb` after the function definition but I don't see how it could be a local variable as it would be reinitialized each time, wouldn't it ?

True, if you let it, but it’s still bad practice for functions to modify outside environment.

Edit: I might as well add this as an afterthought... One way to get rid of global variables is to use an optional function parameter:

Code:
```def dectohex(x, hnumb=None):
# ...
if hnumb is None:
hnumb = []
hnumb.append(hdigit)
if x // 16 == 0:
return ''.join([str(i) for i in hnumb])[::-1]
else:
return dectohex(x // 16, hnumb)```

Last edited by SuperOscar : January 29th, 2013 at 04:10 AM.

#5
January 29th, 2013, 04:44 AM
 chaying
Registered User

Join Date: Jan 2010
Posts: 9
Time spent in forums: 2 h 39 m 46 sec
Reputation Power: 0
Quote:
 “return” without an argument is the same as “return None” which, incidentally, is also the default return value for a function without a return statement, so of course that doesn’t change anything.

Quote:
 But what do you expect the line previous to that does? Put a return before the “dectohex(x/16)” so the function might ultimately actually return something.

It's actually a real misunderstanding of the flow of execution here..
I really didn't see why with the initial statement, the function doesn't end systematically on the return from the if statement.

My understanding now is that without the return statement, it's like I call a new function wich does the same thing but that isn't related to any previous calculation : it looks like it's recursive but it's not. Am I right ?

Quote:
 One way to get rid of global variables is to use an optional function parameter: Code: ```def dectohex(x, hnumb=None): # ... if hnumb is None: hnumb = [] hnumb.append(hdigit) if x // 16 == 0: return ''.join([str(i) for i in hnumb])[::-1] else: return dectohex(x // 16, hnumb)```

Thanks a lot for the tip and your help !

#6
January 29th, 2013, 06:05 AM
 SuperOscar
Contributing User

Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404
Time spent in forums: 1 Week 5 h 17 m 52 sec
Reputation Power: 65
Quote:
 Originally Posted by chaying My understanding now is that without the return statement, it's like I call a new function wich does the same thing but that isn't related to any previous calculation : it looks like it's recursive but it's not. Am I right ?

It’s recursive all right. The main point is what happens after the recursive call is ended and the execution returns to the caller. In your code, the value obtained would simply be discarded. Adding the “return” makes the function ultimately have some end result.

#7
January 29th, 2013, 06:50 AM
 chaying
Registered User

Join Date: Jan 2010
Posts: 9
Time spent in forums: 2 h 39 m 46 sec
Reputation Power: 0
Ok I think I get it now..

I'm still going to run some flows to be sure but so, the ultimate 'return' that I thought would get me the result, only returns it to the last calling function, which holds the result until someone says to her to return it, or that the system discards it : what I didn't do.

I hope I get it right now.. If not, please keep in mind that I do "help 'my' landlady to take out her garbage"..

Thanks again anyway !

#8
January 29th, 2013, 10:18 AM
 SuperOscar
Contributing User

Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404
Time spent in forums: 1 Week 5 h 17 m 52 sec
Reputation Power: 65
Quote:
 Originally Posted by chaying Ok I think I get it now..

No need to call the landlady, you got it right

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Return statement

## Developer Shed Advertisers and Affiliates

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |