Python Forum
Function returns unaccurate value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function returns unaccurate value
#1
This is an example of an issue that I have:

why if a function calls another function the return of the called function is not accurate:

I have inserted some print instructions to evidence the outputs. thank you for your help.
def multiplicacion(a,b):
    producto = a*b
    print producto
    if a>b:
        return producto
    if b>a:
        a += 1
        multiplicacion(a,b)
    print str(producto) + "END"
    return producto


def maths(a, b):
    pluss = multiplicacion(a,b)
    print str(pluss) + " end "

maths(5, 6)
Reply
#2
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.

This is the output I got:

Output:
30 36 36END 30END 30 end
That seems accurate to me. You call maths, maths calls multiplication(5, 6). That goes to line 2, which makes producto 30, which line 3 prints. Then it calls multiplication (6, 6) on line 8. That goes back to line 2, making product 36, which is printed. Then, both if clauses are skipped, because 6 == 6. So line 9 prints 36END. Then producto is returned to the previous call of multiplication, which ignores it. So we go to line 9 again, which prints 30END. 30 (the value of producto in the first call) is then returned to maths, which prints '30 end'.

What were you expecting?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you for your response!

What I expect is the value of "36END" to be returned to math function, but I don't know why it's ignored as you tell, I have tried to put "else" instead of the second "if" in multiplicacion function.
Reply
#4
Like I said, the 36 gets ignored, because you don't do anything with the value of multiplication(a, b) on line 8. There's two ways you could get the 36 at the end. Either assign multiplication(a, b) to producto, or return it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
I just get it, thank you so much!!! I was lost in this like for days, I'm starting to lear python and this site and your help just saved me!

def multiplicacion(a,b):
    producto = a*b
    print producto
    if a>b:
        return producto
    if b>a:
        a += 1
        producto = multiplicacion(a,b)
    print str(producto) + "END"
    return producto


def maths(a, b):
    pluss = multiplicacion(a,b)
    print str(pluss) + " end "

maths(5, 6)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function returns dataframe as list harum 2 1,419 Aug-13-2022, 08:27 PM
Last Post: rob101
  function accepts infinite parameters and returns a graph with those values edencthompson 0 868 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  function that returns a list of dictionaries nostradamus64 2 1,765 May-06-2021, 09:58 PM
Last Post: nostradamus64
  Recursive function returns None, when True is expected akar 0 3,400 Sep-07-2020, 07:58 PM
Last Post: akar
  Python function returns inconsistent results bluethundr 4 3,216 Dec-21-2019, 02:11 AM
Last Post: stullis
  Function returns "NoneType" eoins 3 5,231 May-22-2019, 10:43 PM
Last Post: micseydel
  Function returns memory address Joeicam 1 3,828 Feb-10-2019, 02:23 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020