12 Replies - 510 Views - Last Post: 14 April 2014 - 08:57 PM Rate Topic: -----

#1 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2956
  • View blog
  • Posts: 11,476
  • Joined: 03-December 12

Function not returning value

Posted 14 April 2014 - 05:43 PM

I decided to start learning Python and am working through the book, Data Structures and Algorithms in Python.I am working on an exercise that involves writing a function that returns true on the passing parameter being even. Constraints are you may not use modulo, multiplication, or division.

I have writting the following:

# define function to determine if integer parameter is even or odd
# return true on even. return false on odd.

def is_even(val):

   print(val) # used to ensure val is decrementing
   val = int(val) - 2

   if val == 0:
      print('Hits True') # used to ensure if statement is working
      return True
   if val == 1:
      print('Hits False')
      return False

   is_even(val) # recursive method to decrement the value


num = int(input('Enter a number to determin if number is even: '))

print('IS_EVEN RETURNS {1} on the number {0}'.format(num, is_even(num)))




The comments included are for debugging.

outputs:

Quote

2
Hits True
IS_EVEN RETURNS True on the number 2

3
Hits False
IS_EVEN RETURNS False on the number 3



Now is the strange part...

Quote

4
2
Hits True
IS_EVEN RETURNS None on the number 4

5
3
Hits False
IS_EVEN RETURNS None on the number 5



I am trying to understand why for numbers 2 and 3 it returns as expected, but None on everything else, including 1. I used a print statement to ensure that the if statement was being passed and yet the return does not return the boolean.

This post has been edited by astonecipher: 14 April 2014 - 05:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Function not returning value

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,840
  • Joined: 19-March 11

Re: Function not returning value

Posted 14 April 2014 - 05:55 PM

You're not returning the result if you enter the recursive step. return returns a value to the calling method only, so if you enter 3, you have

top-level calls
>> is_even(3) which calls
>>>> is_even(1) which returns False
<< to is_even, which returns None
to top-level

Was This Post Helpful? 0
  • +
  • -

#3 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2956
  • View blog
  • Posts: 11,476
  • Joined: 03-December 12

Re: Function not returning value

Posted 14 April 2014 - 06:02 PM

I was waiting for you Jon! So I need to reorginize the code a bit.... hmmm

So, I either loose the recursion completely or move it outside of the function making the function pointless.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,840
  • Joined: 19-March 11

Re: Function not returning value

Posted 14 April 2014 - 06:03 PM

Or just return the value that you get from the call on line 16
Was This Post Helpful? 0
  • +
  • -

#5 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2956
  • View blog
  • Posts: 11,476
  • Joined: 03-December 12

Re: Function not returning value

Posted 14 April 2014 - 07:28 PM

Wouldn't returning the call cause the new numeric value to be returned. The recusion would be happening outside of the function? I am looking for a way to do this with recusion, although I know there are better ways, this is what I currently came up with:


# define function to determine if integer parameter is even or odd
# return true on even. return false on odd.

def is_even(val):

   if val < 0:
      val = abs(val)

   while val >= 2:

      while val != 0 and val != 1:

         print(val) # used to ensure val is decrementing

         val = int(val) - 2 # ensure val is a number


   if val == 0:
      return True
   if val == 1:
      return False


#   return val

num = int(input('Enter a number to determine if number is even: '))



print('IS_EVEN RETURNS {1} on the number {0}'.format(num, is_even(num)))  # is_even(num)))



Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,840
  • Joined: 19-March 11

Re: Function not returning value

Posted 14 April 2014 - 07:44 PM

I only see is_even returning booleans. How is that call going to return an int? (I'm still looking at line 16 of your original post)
Was This Post Helpful? 1
  • +
  • -

#7 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2956
  • View blog
  • Posts: 11,476
  • Joined: 03-December 12

Re: Function not returning value

Posted 14 April 2014 - 07:54 PM

	def is_even(val):
	 
	   print(val) # used to ensure val is decrementing
	   val = int(val) - 2
	 
	   if val == 0:
	      print('Hits True') # used to ensure if statement is working
	      return True
	   if val == 1:
	      print('Hits False')
	      return False
	 
	   return is_even(val)



I was so close and just never realized it.

This post has been edited by astonecipher: 14 April 2014 - 07:54 PM

Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,840
  • Joined: 19-March 11

Re: Function not returning value

Posted 14 April 2014 - 07:56 PM

Yep. Usually an indication that you're not diagnosing the problem correctly before trying to fix it.
Was This Post Helpful? 0
  • +
  • -

#9 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2956
  • View blog
  • Posts: 11,476
  • Joined: 03-December 12

Re: Function not returning value

Posted 14 April 2014 - 08:01 PM

Recursion is something I have nerver tested. I have always been told a loop accomplishes the same task with less overhead. I figured learning python, I would also touch on areas I normally overlook.
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,840
  • Joined: 19-March 11

Re: Function not returning value

Posted 14 April 2014 - 08:12 PM

View Postastonecipher, on 14 April 2014 - 10:01 PM, said:

Recursion is something I have nerver tested. I have always been told a loop accomplishes the same task with less overhead. I figured learning python, I would also touch on areas I normally overlook.



This claim about the innate supriority of looping over iteration depends on the scenario. Often the recursive solution is simpler to write, and the scenario is sufficiently finite that you won't run into any problems with overhead. For example, walking a file tree is something you could do nonrecursively, but there isn't any point. You're just not going to have a file tree that challenges your stack - or if you do, then you've got a lot of problems unrelated to your recursion.
Knuth's Law says "premature optimization is the root of all evil" - you should start by writing the code in the clearest and simplest way you can manage, and only then try to address performance issues, if you can find any that matter. (notice that by hypothesis, we're moving away from the maximal clarity to get this performance, so that's certain to involve an increased risk of future errors, so the performance issues had better matter!)
Was This Post Helpful? 0
  • +
  • -

#11 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2956
  • View blog
  • Posts: 11,476
  • Joined: 03-December 12

Re: Function not returning value

Posted 14 April 2014 - 08:37 PM

I still have a lot to learn in several comp sci areas. If it wasn't that I have enjoyed it over the years I would balk at the amount that I know, I don't know.
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,840
  • Joined: 19-March 11

Re: Function not returning value

Posted 14 April 2014 - 08:53 PM

This is a good opportunity for me to uncork one of my standard lectures, dealing with the nature and importance of ignorance. In short, consider the volume of your knowledge as a sphere. As the volume of that sphere increases, its surface area increases as well. That surface area represents the boundary between what you know, and what what you don't, and it's roughly the space of things you know you are ignorant of. Call this your "ignorance" - it's what Rumsfeld referred to as our "known unknowns". These are things, you might say, that you know are important to understand, but you don't yet understand.
The important thing to notice here is that your ignorance always increases faster than your knowledge. This should be obvious: it takes only a moment to learn of your ignorance, and it takes research and study to remedy that ignorance. Furthermore, any research worth doing will always uncover more areas of ignorance than it remedies.

So ignorance is not a bad thing. It's really the opposite: it's a sign that you are learning more, and understanding more, and this is exactly the right thing to be doing. The more you know, the more you know there is to know, and the bigger your horizons get, and the more ignorance you acquire. Revel in it.
Was This Post Helpful? 0
  • +
  • -

#13 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2956
  • View blog
  • Posts: 11,476
  • Joined: 03-December 12

Re: Function not returning value

Posted 14 April 2014 - 08:57 PM

Since we haven't had a pm in awhile, I have forgotten how philosophical you are.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1