Return Functions –

From the above discussion you can understand that, when the function is called with parentheses, the code is executed and returns the result. And, when it is called without parentheses, a function reference is returned to the callable. So, what happens when a function is coded along with a return statement with parentheses and without parentheses. Let’s go through an example.

With Parentheses

Python3




def concatenate_string(*args):
 
    string1 = args[0]
    string2 = args[1]
     
    def merge_string(string1, string2):
        return string1 + string2
     
    return merge_string(string1, string2)
 
def func():
    conc_obj = concatenate_string('Hello, ', 'George')
    print(conc_obj)
 
func()


Output

Hello, George

From the above example, it’s clear that the

merge_string

is a function within the function

concatenate_string

and the main function (

concatenate_string

) returns the sub-function (

merge_string

) to the caller.

 return merge_string(string1, string2) 

Here

merge_string

is invoked with parentheses, hence it executes and provides the result to the

concatenate_string

from where the result is passed to

func

.

Without Parentheses

Python3




def concatenate_string():
     
    def merge_string(string1, string2):
        return string1 + string2
     
    return merge_string
 
def func():
     
    # return the reference of merge_string func
    conc_obj = concatenate_string()
    print(conc_obj)  # prints the reference
 
    # executes the reference
    print(conc_obj('Hello, ', 'George')) 
 
func()


Output:

<function concatenate_string..merge_string at 0x7f1e54ebc158>
Hello, George

Since the

merge_string

is used without parentheses, the

concatenate_string

passes the function reference to the callable

func

rather than executing the

merge_string

.

return merge_string

Hence, we can conclude that when we code sub-function with parentheses in a return statement, the main function executes the sub-function and passes the result to the callable.  And, when we code subfunction without parentheses in a return statement, the main function passes the sub-function reference to the callable rather than executing it. Here the callable decides what to do with the reference.

Python – Invoking Functions with and without Parentheses

Functions in Python are the defined blocks of code that perform a specific task. In this section, we will discuss the difference in invoking functions with and without Parentheses.

  • When we call a function with parentheses, the function gets execute and returns the result to the callable.
  • In another case, when we call a function without parentheses, a function reference is sent to the callable rather than executing the function itself.

Let’s discuss 3 important concepts:

  • Invoking functions
  • Return functions
  • Passing functions

Similar Reads

Invoking functions –

The below function performs a simple task, string concatenation. Here we will invoke the function `concatenate_string` with and without parentheses and see the difference....

Return Functions –

...

Passing Functions –

From the above discussion you can understand that, when the function is called with parentheses, the code is executed and returns the result. And, when it is called without parentheses, a function reference is returned to the callable. So, what happens when a function is coded along with a return statement with parentheses and without parentheses. Let’s go through an example....