How to call a function in 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:

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

Rules for defining a function

  1. The def keyword is used in the Python function to declare and define a function.
  2. The function name must begin with the following identifiers such as: A-Z, a- z, and underscore (_).
  3. Every function must follow colon (:) and then indention to write the program.
  4. In a Python function, the reserved word cannot be used as a function name or identifier.
  5. 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:

Welcome to JavaTpoint

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

  1. Functions can be assigned to a variable
  2. A function is an example of an object type.
  3. We also return the function from a function.
  4. Functions have the same properties and methods as objects
  5. 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

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263574.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论