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 FunctionsFollowing are the features of Python Functions:
Rules for defining a function
Create a function in PythonTo 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 PythonOnce 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 PythonWhen 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 ObjectsIn 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
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