Results 1 to 6 of 6
  1. #1
    Gratis versus Libre johnd's Avatar
    Join Date
    Dec 2004
    Location
    Timaru
    Posts
    1,171

    Default python functions

    Hi

    Have been doing a little beginning learning in Python. All has gone quite well until trying to get my first function to work as in the code below:

    Code:
    #! /usr/bin/python
    
    area=0.0
    
    def Calc_area (width,height):
        area = width*height
        return  area
    
    width = float(input("width?"))
    height = float (input("height?"))
    Calc_area (width,height)
    
    print area
    It does not want to return the area from the function - what am I missing?

    John
    # Linux kernel on:
    - Desktop
    3.0GHz AMD A8-3870
    - Oppo AX7

  2. #2
    Gratis versus Libre johnd's Avatar
    Join Date
    Dec 2004
    Location
    Timaru
    Posts
    1,171

    Default Re: python functions

    If I leave out the line "area=0.0" an error regarding undefined variable is given. If I leave it in, the area variable retains it's global value.
    # Linux kernel on:
    - Desktop
    3.0GHz AMD A8-3870
    - Oppo AX7

  3. #3
    Senior Member
    Join Date
    Jan 2009
    Posts
    170

    Default Re: python functions

    I havn't used python before so please forgive me if this doesn't work/solove your problem.

    just from looking at the code wouldn't you have to call your method against something?

    i.e instead of
    Code:
     Calc_area (width,height)
    try
    Code:
    area=Calc_area (width,height)

  4. #4
    Gratis versus Libre johnd's Avatar
    Join Date
    Dec 2004
    Location
    Timaru
    Posts
    1,171

    Default Re: python functions

    The above code calls the function fine and works out the area - it just does not get back to the main program.
    # Linux kernel on:
    - Desktop
    3.0GHz AMD A8-3870
    - Oppo AX7

  5. #5
    Gratis versus Libre johnd's Avatar
    Join Date
    Dec 2004
    Location
    Timaru
    Posts
    1,171

    Default Re: python functions

    OK - you are of course right. I have not had any experience in any C like language before.
    # Linux kernel on:
    - Desktop
    3.0GHz AMD A8-3870
    - Oppo AX7

  6. #6
    Senior Member MushHead's Avatar
    Join Date
    Jun 2006
    Posts
    513

    Default Re: python functions

    You've got to remember that in nearly all C-like languages, the two "areas" you' have in your example are in fact completely different. Not sure about the case with Python, though the behaviour you describe seems to confirm it.

    So when you remove the "area=0.0" line, you are failing to declare the global "area" variable, so when you "print area", then "area" no longer exists. Similarly, since the "area" within Calc_area() only is in-scope within the function, it contains the calculated area temporarily, then it is thrown away at the "return" statement. So unless you actually use or assign the result of Calc_area(), then you're not achieving anything.
    There are 10 types of people in this world - those who understand binary, and those who don't.

Similar Threads

  1. Search Cursor in Python
    By Mike in forum PressF1
    Replies: 8
    Last Post: 30-04-2010, 10:47 AM
  2. Python Script
    By Faded_Mantis in forum PressF1
    Replies: 1
    Last Post: 18-02-2008, 07:14 AM
  3. Looping in Python
    By Mike in forum PressF1
    Replies: 15
    Last Post: 27-06-2006, 09:03 PM
  4. Replies: 11
    Last Post: 09-06-2004, 11:44 AM
  5. Python
    By jonzsie in forum PressF1
    Replies: 4
    Last Post: 21-02-2003, 10:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •