Closed Thread
Results 1 to 9 of 9

Thread: returning values

  1. #1
    Hot_Milo23's Avatar
    Hot_Milo23 is offline Programmer
    Join Date
    Jun 2009
    Location
    Western Australia
    Posts
    120
    Rep Power
    11

    Smile returning values

    haha, a new question:
    i have a function which currently returns either true or false.
    I currently use it like
    Code:
    if(function_name):
         ...
         ...
    Problem is i now want the function to return a list of values ( as in "[1,2,3]")
    if the function returns true. I'm just wondering if this is possible, and if so, can anyone help me implement it?
    thanks in advance

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: returning values

    Presumably your original function looks something like

    Code:
    def function_name(var):
        if (//some logic):
            return True;
        else
            return False;
    To return a list all you need to do is replace True with the list you want returned if true, and False with the list you want returned if false. ie

    Code:
    def function_name(var):
        if (//some logic):
            return [1,2,3];
        else
            return [4,5,6];
    You then have to assign the return value to a variable

    Code:
    list_var = function_name(somevar);
    If there's a new way, I'll be the first in line.

    But, it better work this time.

  4. #3
    Hot_Milo23's Avatar
    Hot_Milo23 is offline Programmer
    Join Date
    Jun 2009
    Location
    Western Australia
    Posts
    120
    Rep Power
    11

    Re: returning values

    will that still let me use the return value as True?
    Code:
    def function_name():
    if(some_logic):
        return List_Of_Values
    else
        return False
    
    def new_function():
    if(Function_name()):
        code which uses returned values
    else:
        quit()
    does that make more sense, as to what it is im trying to do?
    or maybe:
    Code:
    def function_name():
    if(some_logic):
        return List_Of_Values
    else
        return False
    
    def new_function():
    if(var=Function_name()):
        code which uses var
    else:
        quit()
    would that work better?

  5. #4
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: returning values

    In python any non-zero or non-null variable is evaluated to be true.

    So if you have

    Code:
    def function_name():
        if(some_logic):
            return List_Of_Values;
        else:
            return False;
    you can then use
    Code:
    if (function_name()):
    which will evaluate to True if the list is returned by function_name (assuming it isn't an empty list which would evaluate to False) and will evaluate to False otherwise.

    To use the returned values with in the if statement you would have to first assign the returned value to a variable such as
    Code:
    var = function_name();
    you could then re-write the previous if statement as
    Code:
    if (var):
    If there's a new way, I'll be the first in line.

    But, it better work this time.

  6. #5
    Hot_Milo23's Avatar
    Hot_Milo23 is offline Programmer
    Join Date
    Jun 2009
    Location
    Western Australia
    Posts
    120
    Rep Power
    11

    Re: returning values

    ok, perfect, thx

  7. #6
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: returning values

    You do know that Python doesn't require parenthesis after an if statement, right?

    You can very easily do this:
    Code:
    def function_name():
        if some_logic:
            return List_Of_Values #;
        else:
            return False #;
    Also, what's with ending lines with semicolons? Further, Python cares about whitespace:
    Code:
    def function_name():
    if(some_logic):
        return List_Of_Values
    else
        return False
    
    def new_function():
    if(Function_name()):
        code which uses returned values
    else:
        quit()
    Would need to be:
    Code:
    def function_name():
        if some_logic:
            return List_Of_Values
        else:
            return False
    
    def new_function():
        if Function_name():
            code which uses returned values
        else:
            quit()
    The code you posted would have two empty functions named "function_name" and "new_function", and then random if statements afterward. Not what I think you wanted to to.
    Last edited by ZekeDragon; 10-12-2009 at 12:44 AM. Reason: first code set wasn't edited.
    Wow I changed my sig!

  8. #7
    Hot_Milo23's Avatar
    Hot_Milo23 is offline Programmer
    Join Date
    Jun 2009
    Location
    Western Australia
    Posts
    120
    Rep Power
    11

    Re: returning values

    haha, yes. i posted that off a mobile phone keypad.
    EDIT: oops, no i didn't. haha, no excuses there

  9. #8
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: returning values

    Quote Originally Posted by ZekeDragon View Post
    You do know that Python doesn't require parenthesis after an if statement, right?


    Also, what's with ending lines with semicolons?
    I knew someone would pick up on the parenthesis after I posted it. Still, it's largely a taste and readability thing and, as Milo had posted his code like that, I figured that he is probably more comfortable with that style of code.

    Good spot on the semicolons though. Those were the first lines of python code I've written in months. I seem to have got into the habit of ending all statements with a semicolon without even thinking about it.
    If there's a new way, I'll be the first in line.

    But, it better work this time.

  10. #9
    manux's Avatar
    manux is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    234
    Blog Entries
    1
    Rep Power
    14

    Re: returning values

    Quote Originally Posted by Hignar View Post
    I seem to have got into the habit of ending all statements with a semicolon without even thinking about it.
    **** semicolons

    There is another problem here, it seems he wants to use the returned values, in Python you can't do like in C (if (x=foo()){some code here with x})
    you need to declare x = foo() then if x:do something, else:...die you lousy computer
    BUT, if you are sure you will use lists, you can use a for loop and make f return [] instead of False.
    Here:
    Code:
    def f(x):
      if iOwns:
        return [x,x*2,x*3,x*4]
      else: return []
    for i in f(32):
       #do something...

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. creating a +/- poll (returning multiple values)
    By SuchTheFool in forum PHP Development
    Replies: 5
    Last Post: 06-08-2010, 03:50 AM
  2. Help Returning a Hash
    By BlackMage in forum Java Help
    Replies: 0
    Last Post: 01-18-2010, 05:28 AM
  3. Returning a value
    By moles in forum Python
    Replies: 1
    Last Post: 11-17-2009, 08:18 AM
  4. Not returning Data
    By zeroradius in forum PHP Development
    Replies: 4
    Last Post: 01-30-2009, 03:47 AM
  5. Replies: 34
    Last Post: 10-08-2008, 05:03 AM

Tags for this Thread

Bookmarks

Posting Permissions

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