Page 156 - TP_Plus_v2.2_Class_8
P. 156

Components of Python Function

                  A Python function consists of the following components:
                     Name of the function: A function name should be unique and easy to correlate with the task it will
                    perform. We can have functions of the same name with different parameters.
                     Arguments: Arguments are the actual values that are passed to the function when it is called.
                    These are the values supplied to the function during the function call.
                      Statements: The statements are the executable instructions that the function can perform.
                     Return Value: A function may or may not return a value.


                       Types of Functions in Python
                  We have mentioned earlier that one of the strengths of the Python language is that Python functions
                  are easy to define and use. Python functions can be categorised into built-in functions and user-
                  defined functions. Let us discuss about them in detail.


                  Built-In Functions
                  The print() and input() belong to the category of built-in functions. Python has many built-in functions
                  that are already defined and ready to use like range(), type(), etc.


                  User-Defined Functions
                  User-defined functions are created by the user according to the need of the program. Once the user
                  defines a function, the user can call it in the same way as the built-in functions.
                  The main  difference  between  these  two  categories is  that  built-in  functions  do  not  require  to  be
                  written by us, whereas a user-defined functions are created by the programmer to address specific
                  requirements or perform custom tasks within a program.

                   Program 1: To add two numbers using all the three types of user-defined functions.


                      Program1.py

                   File  Edit  Format   Run    Options   Window    Help

                   #Program that demonstrate to create a user-defined function
                   def add (a, b):
                       c = a+b
                       print("The sum of both the numbers is", c)
                   add (12, 16)



                  You will get the following output:

                      Output

                   The sum of both the numbers is 28







                  154   Plus (Ver. 2.2)-VIII
   151   152   153   154   155   156   157   158   159   160   161