Friday 4 December 2015

Python: functions: return statement

‘return’ statement is used to return a value from function to the caller.


test.py

def factorial(n):
 if(n<=1):
  return 1
  
 result=1
 
 for i in range(2, n+1):
  result *= i
 return result
 
print(factorial(1))
print(factorial(2))
print(factorial(3))
print(factorial(4))
print(factorial(5))

$ python3 test.py
1
2
6
24
120

‘return’ without an expression argument returns None.

def hello():
         print("Hello")

print(hello())

$ python3 test.py
Hello
None


Previous                                                 Next                                                 Home

No comments:

Post a Comment