Python Introduction | Functions | Return Values

You can define a function if you know its name and arguments. However, another important element does not appear in the function name or arguments: the return value.

A return value sends a value back to the caller after the function runs. Use return to specify the value.

Function Definition 2

def function_name(argument1, argument2, ...):
    ...... process to perform ......
    return value

After performing its process, the function returns a value with return value, and the caller receives it.

Let us use a return value. The earlier example can be rewritten as follows.

def showMsg(str): 
    return "Hello," + str + ".How are you?"
   
res = showMsg("Taro") 
print(res) 
res = showMsg("Hanako") 
print(res) 

The showMsg function uses return to send back text. Consider the following call.

res = showMsg("Taro")

The result of showMsg is assigned to the variable res. The code then displays the result by using res.