Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Invoking Function with and without Parenthesis in Python
In Python, function invocation refers to calling or executing a function. Understanding the difference between using functions with parentheses () and without parentheses is crucial for proper function handling and references.
Key Concepts
When you use a function name with parentheses, you invoke (call) the function immediately. When you use a function name without parentheses, you create a reference to the function object without executing it.
Syntax
Function with parentheses (invokes the function) ?
def function_name():
# function body
pass
function_name() # Calls the function
Function without parentheses (creates a reference) ?
def function_name():
# function body
pass
reference = function_name # Creates a reference
print("Function reference:", reference)
Function Invocation with Parentheses
When you add parentheses after a function name, Python immediately executes the function ?
def greet():
print("Welcome to TutorialsPoint")
# Invoke the function with parentheses
greet()
Welcome to TutorialsPoint
Function with Parameters
Functions can accept parameters and return values when invoked with parentheses ?
def addition(x, y):
return x + y
# Call function with arguments
result = addition(30, 40)
print("The sum of two numbers:", result)
The sum of two numbers: 70
Function Reference without Parentheses
Without parentheses, you get a reference to the function object that can be called later ?
def display_message():
print("Python Programming")
# Create function reference without parentheses
func_ref = display_message
print("Function reference:", func_ref)
# Call the function using the reference
func_ref()
Function reference: <function display_message at 0x7f8b8c0b1d30> Python Programming
Returning Function References
Functions can return other functions without invoking them, creating higher-order functions ?
def create_adder():
def add_strings(str1, str2):
return str1 + str2
# Return function reference without parentheses
return add_strings
def demonstrate_reference():
# Get the function reference
adder_func = create_adder()
# Print the reference
print("Function reference:", adder_func)
# Use the reference to call the function
result = adder_func("Hello", " World")
print("Result:", result)
demonstrate_reference()
Function reference: <function create_adder.<locals>.add_strings at 0x7f8b8c0b1e50> Result: Hello World
Comparison
| Usage | Effect | Example |
|---|---|---|
With parentheses ()
|
Invokes/calls the function | func() |
| Without parentheses | Creates a reference to function | ref = func |
Conclusion
Using functions with parentheses immediately invokes them, while without parentheses creates references for later use. This distinction is essential for callbacks, higher-order functions, and dynamic function assignment in Python.
