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.
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.
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)
merge_string is invoked with parentheses, hence it executes and provides the result to the
concatenate_string from where the result is passed to
<function concatenate_string..merge_string at 0x7f1e54ebc158> Hello, George
merge_string is used without parentheses, the
concatenate_string passes the function reference to the callable
func rather than executing the
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.
In this case, the reference of
concatenate_string is passed to the
function_call as an argument.
Inside function_call, it executes the
concatenate_string using the reference and returns the result to the callable.
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
- Mathematical Functions in Python | Set 4 (Special Functions and Constants)
- Check for balanced parentheses in Python
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- SQL | Functions (Aggregate and Scalar Functions)
- Python program to count upper and lower case characters without using inbuilt functions
- Complex Numbers in Python | Set 2 (Important Functions and Constants)
- Complex Numbers in Python | Set 3 (Trigonometric and Hyperbolic Functions)
- Statistical Functions in Python | Set 1 (Averages and Measure of Central Location)
- Python | Set 2 (Variables, Expressions, Conditions and Functions)
- Python | startswith() and endswith() functions
- Python bit functions on int (bit_length, to_bytes and from_bytes)
- Difference between input() and raw_input() functions in Python
- turtle.setpos() and turtle.goto() functions in Python
- Why and how are Python functions hashable?
- Python Regex - re.MatchObject.start() and re.MatchObject.end() functions
- Array in Python | Set 1 (Introduction and Functions)
- maketrans() and translate() functions in Python
- How to load and save 3D Numpy array to file using savetxt() and loadtxt() functions?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.