-1

Below code is printing None, but should be printing a value

num_ex = 3.5

def my_func(x):
    if x >= 3.00:
        x - 1
    else:
        x + 1

var_ex = my_func(num_ex)
print(var_ex)
JackW24
  • 135
  • 9
  • How could it possibly return a value, when it has no `return` statement in it? – jasonharper Oct 20 '22 at 01:10
  • sorry, i meant printing, edited – JackW24 Oct 20 '22 at 01:11
  • 1
    @JackW24 he means that your function definition needs to have `return x` at the end, in order for `my_func(num_ex)` to return to some value to assign `var_ex`. Current it has no return statement, so `my_funct()` evaluates to `None` which makes the expression evaluate to `var_ex = None` – Michael Moreno Oct 20 '22 at 01:15

1 Answers1

2

Every Python user define function return None by default. You are not returning anything that's why it's returning None

To return a value from a function you use the return keyword.

Note: the return keyword terminates the function immediately when encounter.

For Example

num_ex = 3.5

def my_func(x):
    if x >= 3.00:
       return x - 1
    return x + 1

var_ex = my_func(num_ex)
print(var_ex)
Maxwell D. Dorliea
  • 1,086
  • 1
  • 8
  • 20