I created the following function in Python 3.7.
def getFactorial(n):
factorial = 1
for x in range(1, n+1)
factorial = factorial * x
print(factorial)
I am using the Python Shell to output the following commands:
>>>getFatorial(5) returns 120
>>>print(getFatorial(5)) returns None
why does the second command return None?
Because the value of getFactorial(5)
is the one you return from the function.
Since you have no return statement in your function, python returns None
.
(Jul-18-2019, 05:04 AM)leodavinci1990 Wrote: [ -> ]>>>getFatorial(5) returns 120
Using the word "returns" here is wrong - the function doesn't return that value (it returns
None
as mentioned above). What you're seeing is the output from the call to
print
in the function
Some nitpicking about code style:
- It's good to follow naming conventions set for variables and functions in
PEP 8:
Quote:Function names should be lowercase, with words separated by underscores as necessary to improve readability.
Variable names follow the same convention as function names.
mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.
So instead of
getFactorial
one should have
get_factorial
- One should aim to write DRY (Don't Repeat Yourself) code. Therefore
factorial = factorial * x
could be written
factorial *= x
Functional style:
from functools import reduce
from operator import mul
factorial = lambda n: reduce(mul, range(1, n + 1))
print(factorial(5))
Output:
120
I'm afraid >>>print(getFatorial(5)) will return none coz the function you created is on a local scope so factorial is a local variable which can't be used in a global scope unless a global statement is used.
(Jul-18-2019, 05:52 AM)ndc85430 Wrote: [ -> ] (Jul-18-2019, 05:04 AM)leodavinci1990 Wrote: [ -> ]>>>getFatorial(5) returns 120
Using the word "returns" here is wrong - the function doesn't return that value (it returns None
as mentioned above). What you're seeing is the output from the call to print
in the function
Thanks for clearing this up, so I get from this if say that function namely, getFactorial had a return statement in it with a certain data type returned say integer for example, will it return int?
(Jul-19-2019, 01:02 AM)leodavinci1990 Wrote: [ -> ]if say that function namely, getFactorial had a return statement in it with a certain data type returned say integer for example, will it return int?
Not exactly. You return an expression, often just a single name. But there is no set data type for what is returned. You generally design your function to always return the same type, but there are cases where you might plan on returning different types depending on the input. Python gives you the flexibility to do that.
def getFactorial(n):
factorial = 1
for x in range(1, n+1)
factorial = factorial * x
return factorial
The modified getFactorial above always returns an integer, but we might do this:
def getFactorial(n):
if n < 0:
return None
factorial = 1
for x in range(1, n+1)
factorial = factorial * x
return factorial
Since factorial is not defined for negative values, we return None. Now it can return an integer or a None type.