What is the purpose of the return statement? How is it different from printing?

 

Return Statement

Return Statement leads the function to exit and give back the value to its caller after executing the operations in a particular function, As the function has a property of taking something as input as a parameter and giving back the output after executing the operations as return back to the caller of the function.

As shown in the figure that how a function get parameters or we can say input as 7 and perform the action for which the function is designed here it is designed as adding 10 to it and returning the result as 17 to the caller of the function.

 x =0
def function_that_prints():
    print "I printed"
    x++

def function_that_returns():
    return "I returned"
    x++

f1 = function_that_prints()
f2 = function_that_returns()
print "Now let us see what the values of f1 and f2 are"
print f1
print f2

Consider the above example where I used two functions one for giving the output I used to return and in other, I used print statement for the same although both will result in the same.

Whereas in the case of print, the program will run to the next step that is it will increment the value of x by one and move ahead whereas in case of a return the program will not execute the increment in the second function as it will terminate and exit from the function just after returning the value.
As result exit the function so we can use the return only once as it executes and exits. That is the reason the return statement is always advised to write at the end of the function Because after that the value of a variable that is used will not be considered modified as it is already sent to the function caller.

We can store the value of return in a variable and use it again as input of any other or the same function.
Returning the return value to the same function is called recursive, Which works as a loop.

Return Type

The return type is the type of value that the function needs to return it back as the function perforces on a special data type of so it returns the same type of data to the caller. However, we do not need a specific return type in python, but in java it is.

 Print Statement

Print statement in python is used to display the output of or the value of any variable. It prints the output in string form to the console. It is used when we need to print or show some result or any message on the console.

It has an advantage over return as it does not exit from the function after printing So we can use print statements in between the function to show the progress or to show final results.
Whereas we have seen in the previous example where the control got exits from the function after executing the return so after the return whatever is written would not mean the result.
However, print statements are used to print and continue execution but the return is used at the end of the function. Moreover, we can use print statements any number of times in continuation.

Print used the output as a string which goes to the computer machine where it can not be edited as the string is immutable so the output you want to print it will be as it is. Whereas in the case of Return, it gives a value that a human can not see but the computer can and it can be used further in any of the processes as input by simply passing it.

Learn More codecademy

Print prime number

Leave a Comment

%d bloggers like this: