[Tutor] Pure functions in python - need a glimpse of functional programming and recursive functions .
Manprit Singh
manpritsinghece at gmail.com
Fri Aug 14 00:42:30 EDT 2020
Dear sir ,
Consider a problem to find nCr . where nCr = fact(n) / (fact(r) *
fact(n-r)).where fact(x) is factorial of x.
if i write code like this :
def fact(x): # Function
definition for factorial
if x == 0:
return 1
else :
return x * fact(x-1)
def comb(n, k): # Function
definition for finding nCr
return fact(n) / (fact(k) * fact(n-k))
calling function comb as written below
comb(7,2)
returns 21.0 # Which is the
right answer
Just need to know if function comb is a pure function in this particular
case or not . I have raised this question because an external function
fact has been called inside the function comb 3 times .
My second question is, i had never seen any example of recursive
functions in python official documentation. Is it ok to use recursive
functions in python programming ? Do recursive functions take/ consume
more or less resources in comparison to other alternatives in python ?
Regards
Manprit Singh
More information about the Tutor
mailing list