educative.io

What is the role of the return statement in the example code?

Hi,
:raising_hand_woman:t2:
If I delete the return n , The new output is the same as the output of the original code. So I’m confused about the purpose of return n here?



Course: https://www.educative.io/courses/python-fundamentals-for-programmers
Lesson: https://www.educative.io/courses/python-fundamentals-for-programmers/JYmYgLl0Mxg

Hi @Yanchen_Shen,
The purpose of the return n statement in the multiply_by_10 function is to return the modified value of n back to the caller.

In the provided code, num is passed as an argument to the function multiply_by_10. Within the function, n is modified by multiplying it by 10, and then the modified value is assigned to the local variable num. However, since num inside the function is a local variable, it does not affect the value of the global variable num outside the function.

If you remove the return n statement from the function, the function will still execute correctly and print the modified value of num inside the function. However, without the return statement, the function will not pass the modified value back to the caller. As a result, when you call the function multiply_by_10(num), it will not change the value of num outside the function, and the output will remain the same as the original code.

In summary, the purpose of return n is to allow the function to pass the modified value of n back to the caller, enabling the caller to capture and use the modified value if needed.

1 Like