-1

I am very new python programming , while i write the below code inside the if statement the value of a is determined but , after i return to caller , return value is always none , Is there anything missing to return the value inside an if block on a recursive calls .

#! /usr/bin/env python3

def gcd_calc(a, b):
    if(b == 0):
        print(a)
        return a
    c= a%b
    gcd_calc(b,c)



if __name__ == "__main__":
    a, b = map(int, input().split())

    if(a<b):
        print(gcd_calc(a,b))
    else:
        print(gcd_calc(b,a))
PriyabD
  • 21
  • 4
  • 4
    Note, the linked duplicate was the *first hit* when I googled the title of your question... please do some basic research before posting a question. usually, if you google what your title would be, maybe the programming language and "stack overflow" you'll likely find a duplicate. – juanpa.arrivillaga Apr 26 '20 at 09:43

1 Answers1

1

You are missing a return

def gcd_calc(a, b):
    if(b == 0):
        print(a)
        return a
    c= a%b
    return(gcd_calc(b,c)) #you need to return here as you are calling recursively



if __name__ == "__main__":
    a, b = map(int, input().split())

    if(a<b):
        print(gcd_calc(a,b))
    else:
        print(gcd_calc(b,a))
Kuldeep Singh Sidhu
  • 3,748
  • 2
  • 12
  • 22
  • If we have multiple if blocks do we need to return there also ? i was expecting when we return the recursive block ideally terminates at that point . – PriyabD Apr 26 '20 at 09:48
  • when you call something recursively like func >func>func then the first function will give the result of what it gets from second fund, second gives the result of what it gets from 3rd funct, so you need return – Kuldeep Singh Sidhu Apr 26 '20 at 09:50
  • When your method calls itself the return value of that call isnt stored anywhere thats why you need to return that value for every call. – xSparfuchs Apr 26 '20 at 09:52