Jump to content


Photo

I've Got A Question About 'return()' Command


  • Please log in to reply
3 replies to this topic

#1 VOICE RUNNER

VOICE RUNNER

    New Member

  • Members
  • Pip
  • 2 posts

Posted 03 January 2015 - 08:08 PM

first i'm not good at english.. so, i hope you understand my broken english :)

I know the return() command is designed to allow me to arrange for a function i write to provide a value to my program, not display a message!!

however i found this code

def absolute(n):
------if n<0:
--------n = -n
--------return(n)

when i run this code, a number that i put to (n) is displayed on the shell

absolute(6)
6

absolute(-5)
5

what is the reason? i'm so confused :(

Edited by VOICE RUNNER, 03 January 2015 - 08:09 PM.


#2 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 05 January 2015 - 10:51 AM


Hi VOICE RUNNER ! Your English is fine. I have heard and read way more horrible English. Mine is not much developed either. :lol:

Always remember to post codes using <> button in the editor

or

use

[code]
paste your code here
[/code]


Any way, the return statement of Python has nothing to do it but it is the shell itself which is doing that. The shell prints the object, which is a result of an operation on the command line.

e.g.,
>>> def add(x,y):
...     return x+y
... 
>>> add(4,5)
9

In IPython console too but in a pretty way.

In [1]: def add(x,y):
   ...:     return x+y
   ...: 

In [2]: add(5,6)
Out[2]: 11


Forget the functions, just take the below codes.

>>> s = 'hello'
>>> s             #will print the string
'hello'

or

>>> 6 + 6       #Just like calculator
12

If you write those codes in an IDE or even IDLE (the default Python IDE) and save it and run; it will not print anything until you use print statement. Try it. (Don't copy that ">>>" it is the prompt :P ) This is the default behavior of shell. But don't worry; because normally a working code doesn't get executed in the interactive shell. So, nothing will be printed. Just use a good IDE or IDLE to get a hang of it.

Just to give you some extras, you can control this default behavior by either assigning the return value to a variable and print that according to your wish. (easy way)

or

a bit clumsy way is to change the default behavior of shell from inside of Python.
like

>>> import sys
>>> sys.displayhook = lambda s: None
>>> 6 + 6
>>> print 6 + 6
12
I would NOT ENCOURAGE to use the above code because python interactive shell is often used as a debugging tool by programmers. Just test the portion or a function of your code you want to debug and you don't have to put a lot of 'print statements' in your program and later find and delete them. For easy copy pasting ;) and debugging with python shell, use DreamPie

If you are more interested about the "displayhook from sys module" read displayhook

B)

Edited by #TM#, 05 January 2015 - 11:16 AM.


#3 VOICE RUNNER

VOICE RUNNER

    New Member

  • Members
  • Pip
  • 2 posts

Posted 06 January 2015 - 04:50 AM

Thank u so so much !! now i understand almost every question i have had



and by the way, is the return() command always used only in the function?

#4 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 09 January 2015 - 07:50 AM


Glad to know it helped.

Yes, return statement is used in a function. In most programming languages there are return statements. To understand that we need to go a little into computer science.

When the function is called (CALLED NOT DEFINED) the program enters into another subroutine leaving the current subroutine/main program to do a specific work defined by the function/subroutine. To come out of the subroutine either wait till the execution of subroutine ends or return out of the subroutine after achieving desired results to the point next to where the subroutine was called.

Bit clumsy to understand. :rolleyes:
See the below pic. Probably you will get the idea.

Posted Image

There is also a WIKIPEDIA LINK to understand it better.

A small example.

def large(m,n):
    """Compare two numbers and returns the larger one."""
    if m == n:
        return m                   #either return m or n; doesn't matter.
    if m > n:
        return m
    return n

print large (40,5)
:) B)

Edited by #TM#, 09 January 2015 - 07:58 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users