Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 131 - 2620_Birla Open Mind C-8
P. 131

    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

                     Built-in functions are functions that are already part of the Python language. You don't need to define
                     them yourself; you can use them directly in your code.

                     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 2: To add two numbers using user-defined function
                         Program2.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

                             Double Tap                                                  Century   #Information Literacy
                                                                                            21 st
                                                                                           Skills
                           1.  Write any two components of Python function.

                           2.  Write the main difference between built-in and user-defined functions.

                                                                                          #Functions and Strings in Python 129

| 126 127 128 129 130 131 132 133 134 135 136 |