Invoking Function with and without Parenthesis in Python


The term invoking function refers to recursion which means the function calls itself. The function with parenthesis defines some parameter to work on specific conditions and operations whereas without parenthesis function refers to a function reference. In Python, we have two types of methods such as function reference and function with parameter that will be used to solve the Invoking function with and without Parenthesis.

Syntax

The following syntax is used in the examples

def function_name():
--------
--------
function_name()

This is the representation of function with parenthesis.

def function_name():
--------
--------
reference = function_name
print("The function reference:", reference)

This is the representation of function without parenthesis with the help of reference.

Simple Invoking Function with Parenthesis

The program uses a recursive function in the following example and applies the print statement to display some text. Then use the calling function to return the result.

Example

# invoke the function without parenthesis
def education():
    print("Welcome To Tutorialspoint")
education()

Output

 Welcome To Tutorialspoint

Invoke Function with Parameter

In the following example, the program uses a recursive function named addition() that accepts two parameters− x and y that will work on adding two variables. Next, use the calling function that passes two values w.r.t x and y, and store it in the variable res. Finally, it will print the result with the help of variable res.

Example

def addition(x, y):
    return x + y
res = addition(30, 40)
print("The sum of two numbers: ", res)

Output

 The sum of two numbers:  70

Assign the Function Reference without Parenthesis

In the following example, the program uses a function definition named Emp() and applies print to display some text. Next, it will use the function reference where function Emp stores it in the variable result. Now use the calling function to return the output.

Example

def Emp():
    print("Python Codeground")
# function reference without parenthesis
result = Emp
result() 

Output

 Python Codeground

Function Return without Parenthesis

In the following example, the program uses three functions namely− add_string(), merge_stng(), and, without_parenthesis(). The second function merge_stng accepts two parameters that will be used to add two strings. Next, the merge_stng returns the result without parenthesis. The records of merge_stng will be stored in the function named add_string. Moving ahead to use another function i.e. without_parenthesis() that set the reference of merge_stng function using the function add_string() and stores it in the variable res. Now it will print the reference along with its execution. Finally, we are calling the function named without_parenthesis() to get the result.

Example

def add_string():
	def merge_stng(str1, str2):
		return str1 + str2
# return the function without parenthesis	
	return merge_stng
def without_parenthesis():
	# return the reference of merge_string function
	res = add_string()
	# prints the reference
	print(res) 
	# executes the reference
	print(res('Welcome', ' to India'))
without_parenthesis()

Output

<function add_string.<locals>.merge_stng at 0x7f176a50b910>
Welcome to India

Conclusion

We discussed the four different methods to work on Invoking function with and without the Parenthesis of Python. There was no use of any built−in function to perform these tasks. The Invoking function is an amazing technique to reduce the length of the code and take advantage of the iteration.

Updated on: 14-Aug-2023

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements