#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    14
    Rep Power
    0

    arguments in python


    i am learning python, and i found this arg. example in a tutorial:

    def twice(x):
    'apply + operation to argument'
    return (x + x)

    how to i get it to return a value? i type in 'python twice.py' (the program's name is 'twice'), but nothing happens. i try:
    'python twice.py twice(3)', but again, nothing happens. what am i doing wrong?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Run that program in the Python Interpreter. When returning a value, a program will not show it because it is "invisible". If you want to show the value in a program, type print twice(3). Other than that, return is used for returning a value that is to be used for another process, command, etc. I think you get the idea now.
  4. #3
  5. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    While Yegg's explanation is correct, it could use some elaboration. The main issue here is that when you define a function, that is all you are doing; in order for the function to actually run, it has to be called first. You can do this by adding the line

    Code:
    print twice(3)
    after the end of the function (making sure that it is not indented; otherwise, it will be interpreted as part of the function, even though it would never run because of where the return statement is). This would give the full code for the program as:

    Code:
    def twice(x):
        'apply + operation to argument'
        return (x + x)
    
    print twice(3)
    When you run it at the listener (the interpreter's interactive mode prompt), you are essentially running it as if it were a program consisting of that one line, and the listener automatically shows whatever the final value of that expression was, if any.
    Last edited by Schol-R-LEA; April 3rd, 2005 at 10:48 PM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

IMN logo majestic logo threadwatch logo seochat tools logo