How to Call a Function in Main Python
How to call a function in Python?
As we know, functions are the block of statements used to perform some specific tasks in programming. It also helps to break the large group of code into smaller chunks or modules. Functions can be called anywhere and the number of times in a program. It allows us to reuse the code by simply calling the particular function or block in a program. Thus, it avoids the repetition of the same code. We can define functions inside 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 need to use a def keyword to declare or write a function in Python. Here is the syntax for creating a function:
Syntax
Let's create a function program in Python.
Myfun.py
Output:
Function Calling in Python
Once a function is created in Python, we can call it by writing function_name() itself or another function/ nested function. Following is the syntax for calling a function.
Syntax:
Consider the following example to print the Welcome Message using a function in Python.
CallFun.py
Output:
Hello World Welcome to the JavaTpoint
In the above example, we call the MyFun() function that prints the statements.
Calling Nested Function in Python
When we construct one function inside another, it is called a nested function. We can create nested functions using the def keyword. After creating the function, we have to call the outer and the inner function to execute the statement. Lets' create a program to understand the concept of nested functions and how we can call these functions.
Nest.py
Output:
Hello, it is the outer function Hello, it is the inner function
As we can see in the above example, the InFun() function is defined inside the OutFun() function. To call the InFun() function, we first call the OutFun() function in the program. After that, the OutFun() function will start executing and then call InFun() as the above output.
Note: To call an inner function, we must first call the outer function. If the external function is not invoked, the inner function will not be executed.
Program to print the multiplication of two numbers using the nested function in Python.
Nest_arg.py
Output:
Display the value of outer variable 6 Display the sum of inner function 8
Functions as First-Class Objects
In Python, the functions as First-Class Objects. Because it treats the same as the object, and it has the same properties and method as an object. A function can be assigned to a variable, pass them as an argument, store them in data structures and return a value from other functions. It can be manipulated, such as other objects in Python. Furthermore, all the data in the Python program is represented in the objects or relations. Hence it is also called first-class 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 program to understand Python functions as an object.
Obj.py
Output:
WELCOME TO JAVATPOINT HELLO, WELCOME TO JAVATPOINT
Write a program to call a function inside the class.
Student.py
Output:
Roll no. is 101 Name of student is Johnson
How to Call a Function in Main Python
Source: https://www.javatpoint.com/how-to-call-a-function-in-python
0 Response to "How to Call a Function in Main Python"
Post a Comment