def f(n):
if n == 1:
return 1
return n + f(n-1)
print(f(2))
Answer will be 3
------------------------------------
def f(n):
if n == 1:
return 1
return n + f(n-1)
Answer will be None
when you run this snippet on a compiler it prints out None.
Even if the indentation is corrected you can not have 2 results in one function unless it is separated by an else like below
def get_absolute_value(num):
if num < 0:
return -num
else:
return num
result = get_absolute_value(-5)
print(result) # Output: 5
It's a recursive function that adds up all the numbers from n, n-1, n-2 ,...,1. So, given n=2, and assuming the indentation is correct, the result will be: 2 + 1 = 3.
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
46a1b89
1 month agoalopezme
8 months agoblaze056
10 months, 3 weeks agoTheFivePips
11 months, 3 weeks agoswyyuen
1 year, 1 month agoemanuelcar990
1 year, 3 months agoowenmagas
1 year, 3 months agoCC_DC
1 year, 4 months agoEllo2023
1 year, 4 months agoEllo2023
1 year, 4 months agoEllo2023
1 year, 4 months agodavid0001
1 year, 9 months agorotimislaw
2 years agoRam5678
2 years, 1 month agoJO5H
2 years, 1 month agoalfonsocav1982
2 years, 2 months agoJnanada
2 years, 3 months ago