Why the return statement is not printed only the print statement is printed

e = ‘mathematical constant’
def exponential(x):
e=2.72
print(e)
return e**x

result=exponential(5)
print(e)

the output is
2.72
mathematical constant

my doubt is why the return statement value(e**x)is not printed

The print() function prints the specified message to the screen, or other standard output device.
The return statement statement does not print out the value it returns when the function is called. It however causes the function to exit or terminate immediately, even if it is not the last statement of the function. So the returned value from a function can be assigned back to a variable.

Consider this:
a function that prints the addition of two numbers

def add_num(x, y):
    print(x+y)

When this function is called, it will print the addition of the parameters, it will also return a None

>>>add_num(3,4)
7
>>>total = add_num(3,4)
7
>>>print(total)
None

Returning a value

def add_num2(x, y):
    return x+y

This function will return a value when it is called, the value can be assigned to a variable.

>>>total2 = add_num2(3,4)
>>>print(total2)
7

Resources

^1
^2
^3

1 Like

To answer your question, simply :

print(result)


PS: To make your code look like this:

e = ‘mathematical constant’
def exponential(x):
    e=2.72
    print(e)
    return e**x

result=exponential(5)
print(e)

just wrap your code in:

            ```python

                # Your code here!

            ```

that would make it look a lot prettier :slight_smile:

1 Like