0

What happens if a function reach "return" statemt.

When function execution encounter "return statement: does the function outputs the value and stop execution OR it give back the value and stop execution. In this case, does the sentences "output the value" and "return the value back" has the same meaning?

10th Mar 2025, 10:14 AM
Oliver Pasaribu
Oliver Pasaribu - avatar
2 Antworten
+ 2
Let me give you an analogy: Suppose there is a boss and a worker. The boss asks the worker to perform a quick calculation (for example, 2 + 2). After completing the calculation, the worker reports the value to the boss, who writes the value on a whiteboard. In this situation, the boss is the "Caller," and the worker is the "Function." The worker reporting the value to the boss is referred to as a "Return." In programming, if a program executes a function, the program is the "Caller." When the function encounters a return statement, it returns the value to the Caller. The Caller receives the value and then carry on with its operations, but the function is terminated. It's important to note that "output" is not the same as "return." If a program executes a function and the function doesn't have a return statement but contains only a print() statement, this function will print (output) the value on the screen, and nothing is returned to the Caller. However, the Caller will continue its operations as the function completes its task.
10th Mar 2025, 12:49 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Oliver Pasaribu , since you have finished 2 python courses, i will give a sample code in python which uses a `return` statement. see also the *comments* to this code. https://sololearn.com/compiler-playground/ctmKl4OuXwon/?ref=app
10th Mar 2025, 4:13 PM
Lothar
Lothar - avatar