Learn Python – How to call a function in Python?- Basic and advance

As we know, functions are the block of statements used to perform some specific duties in programming. It additionally helps to spoil the giant crew of code into smaller chunks or modules. Functions can be known as somewhere and the wide variety of instances in a program. It permits us to reuse the code with the aid of genuinely calling the particular function or block in a program. Thus, it avoids the repetition of the same code. We can outline features interior the class, modules, nested functions, etc.

Features of Functions

Following are the features of Python Functions:

It is used to avoid repetitions of code.
Using the function, we can divide a group of code into smaller modules.
It helps to hide the code and create clarity to understand the modules.
It allows code to be reusable, thus saving memory.
Statements written inside a function can only be executed with a function name.
Python function starts with def and then a colon (:) followed by the function name.

Rules for defining a function

The def keyword is used in the Python function to declare and define a function.
The function name must begin with the following identifiers such as: A-Z, a- z, and underscore (_).
Every function must follow colon (:) and then indention to write the program.
In a Python function, the reserved word cannot be used as a function name or identifier.
In Python, the function parameter can be empty or multiples.

Create a function in Python

To create a function, we want to use a def key-word to declare or write a function in Python. Here is the syntax for growing a function:


def function_name(): # use def keyword to define the function  
Statement to be executed  
return statement # return a single value.  

Let’s create a function program in Python.


def myFun(): # define function name  
    print(" Welcome to JavaTpoint")  
myFun() # call to print the statement  


Welcome to JavaTpoint

Function Calling in Python

Once a characteristic is created in Python, we can call it through writing function_name() itself or any other function/ nested function. Following is the syntax for calling a function.


def function_name():  
function_name() # directly call the function  
# calling function using built-in function   
def function_name():   
str = function_name('john') # assign the function to call the function  
print(str) # print the statement  

Consider the following example to print the Welcome Message the usage of a characteristic in Python.


def MyFun():  
    print("Hello World")  
    print(" Welcome to the JavaTpoint")  
MyFun() # Call Function to print the message.  


Hello World
 Welcome to the JavaTpoint

In the above example, we name the MyFun() characteristic that prints the statements.

Calling Nested Function in Python

When we construct one function internal another, it is referred to as a nested function. We can create nested functions using the def keyword. After creating the function, we have to name the outer and the internal feature to execute the statement. Lets’ create a software to recognize the thinking of nested functions and how we can name these functions.


def OutFun(): # outer function  
    print("Hello, it is the outer function")  
    def InFun(): # inner function  
        print("Hello, It is the inner function")  
    InFun() # call inner   
OutFun() # call outer function      


Hello, it is the outer function
Hello, it is the inner function

As we can see in the above example, the InFun() feature is described inner the OutFun() function. To name the InFun() function, we first name the OutFun() characteristic in the program. After that, the OutFun() feature will begin executing and then call InFun() as the above output.

Note: To name an internal function, we have to first call the outer function. If the exterior characteristic is no longer invoked, the inner function will not be executed.

Program to print the multiplication of two numbers the use of the nested characteristic in Python.


def fun1(): # outer function  
    a = 6 # define variable   
    def fun2(b): # inner function  
        a = 4 # inner variable   
        print ("Display the sum of inner function", a + b) # sum of inner function  
    print ("Display the value of outer variable", a) # it displays the value of outer function    fun2(4)  # Inner function   


Display the value of outer variable 6
Display the sum of inner function 8

Functions as First-Class Objects

In Python, the features as First-Class Objects. Because it treats the same as the object, and it has the equal houses and approach as an object. A characteristic can be assigned to a variable, bypass them as an argument, store them in data buildings and return a fee from other functions. It can be manipulated, such as different objects in Python. Furthermore, all the records in the Python application is represented in the objects or relations. Hence it is additionally referred to as nice citizens of Python function.

Properties of First-Class functions

Functions can be assigned to a variable
A function is an example of an object type.
We also return the function from a function.
Functions have the same properties and methods as objects
The function is treated as an object to pass as an argument to another function.

Create a software to recognize Python functions as an object.


def MyObject(text): # Pass an argument.  
    return text.upper()  
# Call the function inside the print() function.  
print (MyObject("Welcome to JavaTpoint"))  
str = MyObject # assign the function to a variable  
# call the function using the str variable.  
print (str("Hello, Welcome to JavaTpoint"))  



Write a program to name a characteristic inner the class.


class Student:  
    Roll_no = 101  
    name = "Johnson"  
    def show(self):  
        print(" Roll no. is %d\nName of student is %s" % (self.Roll_no, self.name))  
stud = Student() # Create the stud object of Student class  
stud.show()   # call the function using stud object.     


Roll no. is 101
Name of student is Johnson