Related Articles

# Python – Invoking Functions with and without Parentheses

• Difficulty Level : Easy
• Last Updated : 08 May, 2020

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

## 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.

 `def` `concatenate_string(``*``args):``    ``string1 ``=` `args[``0``]``    ``string2 ``=` `args[``1``]`` ` `    ``return` `string1 ``+` `string2`` ` `obj ``=` `concatenate_string(``'Hello, '``, ``'George'``)``print``(``'Function call with Parentheses: '``)``print``(obj)`` ` `obj ``=` `concatenate_string``print``(``'Function call without Parentheses: '``)``print``(obj)`

Output

```Function call with Parentheses:
Hello, George
Function call without Parentheses:
<function concatenate_string at 0x7f0bb991df28>
```

For the first case, when we call `concatenate_string('Hello, ', 'George')`, the function executes and returns the concatenated string.
And, for the second case, when we call `concatenate_string` i.e. without parentheses, a reference is passed to the callable rather than executing the function itself. Here the callable can decide what to do with the reference.

## 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

 `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

 `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.

## Passing Functions –

You can pass a function as an argument by creating the reference, calling the function without parentheses, and provide it as an argument. Let’s look into the code.

 `def` `concatenate_string(``*``args):``    ``string1 ``=` `args[``0``]``    ``string2 ``=` `args[``1``]`` ` `    ``def` `merge_string(string1, string2):``        ``return` `string1 ``+` `string2  ``# string merge``    ` `    ``# executes merge_string and return the result``    ``return` `merge_string(string1, string2)`` ` `def` `function_call(function):``    ``string1 ``=` `'Hello, '``    ``string2 ``=` `'George'``    ``return` `function(string1, string2)`` ` `# passing function as argument``print``(function_call(concatenate_string))  `

Output:

`Hello, George`

In this case, the reference of `concatenate_string` is passed to the `function_call` as an argument.

``function_call(concatenate_string)``

Inside function_call, it executes the `concatenate_string` using the reference and returns the result to the callable.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.53727716,"math_prob":0.74851996,"size":4854,"snap":"2021-31-2021-39","text_gpt3_token_len":1086,"char_repetition_ratio":0.23298968,"word_repetition_ratio":0.13333334,"special_character_ratio":0.2171405,"punctuation_ratio":0.12353706,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95189303,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-18T22:26:38Z\",\"WARC-Record-ID\":\"<urn:uuid:6ffa85d4-4b70-4654-84e5-2e9e10d0fb39>\",\"Content-Length\":\"122237\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:642312a3-4bfc-425c-8ae1-f363c1b37622>\",\"WARC-Concurrent-To\":\"<urn:uuid:39c8179c-6786-49a0-8bc4-8a4837949eff>\",\"WARC-IP-Address\":\"23.12.145.61\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/python-invoking-functions-with-and-without-parentheses/\",\"WARC-Payload-Digest\":\"sha1:QZAEIYMUCCGZ2NOI477QOGPKQJJJWVZX\",\"WARC-Block-Digest\":\"sha1:FMD3K7NVMTXCHUL6OYVKEUCO5M56JFLZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780056578.5_warc_CC-MAIN-20210918214805-20210919004805-00131.warc.gz\"}"}