Defining Functions

Invoking Functions

A function in Python refers to an aggregation of related statements designed to facilitate the performance of a logical, evaluative, or computational task. Also, invoking functions means to allow something to run them. Moreover, experts define functions in Python, which is one of the programming languages, as the defined blocks of code that facilitates the performance of a specific task.

invoking functions

                                                                                                                                 Invoking Functions

Invoking Function With and Without Parentheses

For the purpose of understanding invoking functions, one must look at the below function that performs a simple task known as string concatenation. Here we will see how one can invoke the function `concatenate_string` with and without parentheses. Moreover, one would be able to answer the popular- what are the two ways of invoking functions.

                    
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 the use of Parentheses:

Hello, George

Function call without the use of Parentheses:

 

For the first case, when we call concatenate_string(‘Hello, ‘, ‘George’), the concatenated string is executed and returned by the function.

For the second case, when the calling of concatenate_string i.e. without parentheses takes place, the passing of a reference takes place to the callable rather than executing the function itself. Moreover, the callable is in a position to decide what to do with the reference.

From the above one can see that when the function is called with parentheses, the code is executed and the result is returned. Furthermore, without parentheses, the returning of a function reference takes place to the callable.

Browse more Topics Under Defining Function

With Parentheses

With regards to invoking functions, what would happen in case a function is coded along with a return statement with parentheses? Below is an example.

 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

From the above example, it becomes certainly clear that the merge_string is a function that is within the function concatenate_string while the main function (concatenate_string) returns the sub-function (merge_string).

return merge_string(string1, string2)

Here merge_string is invoked with parentheses. As such, the result is provided to the concatenate_string and then the result is passed to func.

Without Parentheses

With regards to invoking functions, what would happen in case a function is coded along with a return statement without parentheses? Below is an example.

                    
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:

Since the use of merge_string takes place without parentheses, the concatenate_string passes the function reference to the callable func rather than executing the merge_string.

return merge_string

Hence, one can say that when the sub-function is coded with parentheses in a return statement, the main function facilitates the execution of the sub-function. Moreover, this execution takes place by the main function and the passing of the result takes place to the callable.

When the coding, in a return statement, of the subfunction happens without parentheses, the passing of the main function takes place to the sub-function reference to the callable instead of executing it. Moreover, here the callable is able to decide what to do with the reference. This is certainly important for invoking functions.

FAQs For Invoking Functions

Question 1: Simply explain how does calling a function in python can occur?

Answer 1: One can call a function from another function, program or the python prompt after defining it. Most noteworthy, in order to call a function, one would have to type the function name with appropriate parameters.

Question 2: What is the use of parenthesis in python?

Answer 2: The parentheses help in the forcing of an order of operations and in invoking functions. Furthermore, in case someone has an additional part in their conditional, parentheses would be useful as it would indicate which or that and paired with.

Share with friends
Customize your course in 30 seconds

Which class are you in?

5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started
Customize your course in 30 seconds

Which class are you in?

No thanks.