Page 99 - Digicode_AI_class_8
P. 99

Built-In Functions
            The print( ) and input( ) belong to the category of built-in functions. We also have other built-in
            functions like range( ), type( ), etc. The main difference between these two categories is that built-in
            functions do not require to be written by us whereas a user-defined function has to be developed by
            the user at the time of writing a program.

            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.  User-defined
            functions are divided into various categories based on the parameters and return type. The functions
            that do not take any parameter or return anything are called type 1 functions.
            The  type  2 functions  take parameters  but  do
            not  return  anything.  The  type  3  functions  take
            parameters and return output.                          def keyword is used to define the functions.



                                                                                  Information Literacy
                   code Quest                                                     Media Literacy



                   1.  Write any two components of Python function.


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







            Creating a Function

            We can create a function in the following ways:

               Defining a Function: We use the def keyword to begin the function definition.
               Naming a Function: Provide a meaningful name to your function.

               Supply  Parameters: The  parameters  (separated  by  commas)  are  given  in  the  parenthesis
               following the name of the function. These are basically the input values we pass to the function.
                 Body of the function: The body of the function contains Python statements that make our function
               perform the required task. Syntax of creating a function is:

                def < name of the function > (list of parameters)
                       <body>

            Calling a function

            A  function  can  be  called  anytime  from  other  functions  or  from  the  command  prompt  after  the
            definition. For calling a function, we type the function and pass the parameters.

                                                                                    Functions in Python   97
   94   95   96   97   98   99   100   101   102   103   104