Get Unlimited Contributor Access to the all ExamTopics Exams!
Take advantage of PDF Files for 1000+ Exams along with community discussions and pass IT Certification Exams Easily.
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.
alopezme
1 month agoblaze056
3 months, 3 weeks agoTheFivePips
4 months, 3 weeks agoswyyuen
6 months, 1 week agoemanuelcar990
8 months, 1 week agoowenmagas
8 months, 3 weeks agoCC_DC
9 months, 2 weeks agoEllo2023
10 months agoEllo2023
10 months agoEllo2023
10 months agodavid0001
1 year, 2 months agorotimislaw
1 year, 5 months agoRam5678
1 year, 6 months agoJO5H
1 year, 7 months agoalfonsocav1982
1 year, 8 months agoJnanada
1 year, 8 months agoMarkBell
1 year, 11 months ago