# I completely don't understand how the output happened

The only thing that I understand that the output ( mathematical constant ) is from print(e).

But I don’t understand :

1- how the output ( 2.72 ) happened [ and I think the output ( mathematical constant ) should be the first output because of the print function ].

2- I think the output of result_2=devide() should be 1000 / 50 = 20 because I have typed return a_sum / lenght inside the function body.

1 Like

HI @Maho,

You must be knowing that python codes get executed from top to bottom. But when there are function definitions (`def function_name()`), those line of code wont get executed until that function is called.

So with that understanding let us look at the code again.

The code is running from top to bottom and the first line that produces an output will be `result = exponential(5)` . When this line gets executed the function gets called and executed in the order of lines inside the function. So the first output it produces will be `print(e)` and it uses the variable `e` declared within the function to execute this line.

If you’re wondering why it didn’t take the `e` value mentioned at the beginning, check the mission screen where it explains the reason.

The temporary memory associated with a function is isolated from the memory associated with the main program. The consequence of this is that we can initialize a variable `x = 10` in the main program, and then execute `x = 3.14` in the body of a function without overwriting the `x` variable of the main program.

So here the first output will be from print(e =2.72) --> 2.72
The next line gets executed and the value is returned to the function, but it is not printed since there is no code asking it to print.

Then the next line runs which is `print(e)` which is outside the function. Due to global scope of variable `e` declared at line one (and due to the local scope of `e` within the function isn’t available outside the function) what it will print is going to be print(e='mathematical constant)

Now the next line that gets executed is `result_2=devide()` which will call the function and the 2 print statements will be executed and give the output 1000 and 50, and returns the divided value to the function but it is not getting printed due to lack of print() command.

I hope this helps.

PS: Next time pls copy and paste the actual code here. It makes it easier to reply to your questions than typing all the code looking at the screenshot.

3 Likes

I still don’t have answers to my questions.

can you please provide the code?

e = ‘mathematical constant’
a_sum = 1000
length = 50

def exponential(x):
e=2.72
print(e)
return e**x
result=exponential(5)
print(e)

def devide():
print(a_sum)
print(length)
return a_sum / length
result_2=devide()

This e has a global scope which means its available throughout the code

These also have a global scope.
So far line 1,2 and 3 are executed and values are assigned to the above variables.

A function doesn’t get executed unless it is called. So the above code doesn’t get executed yet since it wasn’t called yet.

This line calls the function and stores the value it returns to `result`. So when the function is called, the lines inside the function gets executed sequentially which is

So now we are inside the function. Since 2.72 is assigned to `e`, now the value of `e` becomes 2.72. But do note that, this variable has a local scope and a stored in a temporary memory which is active only as long as the function is running. So it will not be available outside the function.

Outside the function, the value of `e` is still `e = ‘mathematical constant’` because of # line 1 and its global scope and stored in a more permanent sort of memory. (This can be overwritten if you assign another value to `e` outside the function body )

So now the next line in function gets executed which is `print(e)`. If you have followed till here, you will know the it is going to print the value of `e` within the function which is 2.72 which is the first output.

Now the next line within the function gets executed `return e**x`. This line will calculate value and returns this value back to the function. So this function prints the value of `e` and returns another float value which is the exponential value. But there are no print command to print out this result. So the value returned by the function doesn’t get printed as an output, but silently saved into `result` variable. We are not printing `result` variable as well. (You can try and print result` and see its output)

So far the only output has been 2.72.

Since we had already reached the `return` line of the function, we exit from the function and move to the next lines of codes outside of the function `print(e)`.

Since we are outside the function, e=2.27 is no longer valid. So `e` looks for its value and finds it at line #1 `e = ‘mathematical constant’` and prints out this values which is the second line of output.

After running this line, the code continue to run the next lines.

The next lines of code are

Like you have already understood, these lines won’t get executed until this function is called. So it doesn’t get executed until it reaches the last line `result_2=devide()`

This line calls the function and stored its value to a variable called `result_2`
So we are inside this function now and it will get executed line by line.
If you have followed till here, you wouldn’t have any difficulty in understanding that these print functions will output 1000 and 50 respectively. Just like in the previous function, since the value returned by the function is not printed, it will not show the result of division. But it will be returned to `result` variable. (You can try printing this variable and check yourself. You will see all the outputs of that function by doing so)

So now you have 4 lines of output as given in your screenshot.
I hope this made some sense.

2 Likes

Thanks a lot, @jithins123.
I thought that the only output of any created function is what after the return statement and anything before the return statement doesn’t appear as an output, but now I understand from your explanation that also if I type print function before the return statement inside the function’s definition, the argument of the print function will appear as an output if I call the function that I have created.
Am I right?

Yes, you can add a print() function to print the result within the function if you want to. But it can also be achieved by printing the variable to which the function call is assigned like printing result, result_2 in the above case.

1 Like

Thanks a lot, @jithins123.