Page 253 - Computer Science V2.0 Class 11
P. 253

Suppose, the module geometry.py is stored in the folder D:\classXII\Ch1. Next, suppose we wish to use the module
                 geometry.py for computing the areas of triangles and rectangles.
                 For this purpose, we need to import these functions from the module geometry as follows:

                 import sys
                 sys.path.append('D:\classXII\ch1')
                 from geometry import areaTriangle, areaRectangle
                 Next, we demonstrate how the module geometry may be used from the Python shell:

                  >>> import sys
                  >>> sys.path.append('D:\classXII\ch2')
                  >>> from geometry import areaTriangle, areaRectangle
                  >>> areaTriangle(5, 6)
                          15.0
                  >>> areaRectangle(5, 6)
                          30


                      Let's Summarise


                   Ø   This process of dividing a computer program into separate independent blocks of code or sub-programs with
                       different names and functionalities is known as modular programming.
                   Ø   Each independent unit of code is called a module.

                   Ø   A function is a sub-program that acts on data to perform a particular task and often returns a value.
                   Ø   A function is always defined before it is called.
                   Ø   Functions can be broadly classified into user-defined functions, built-in functions and functions defined in
                       modules.
                   Ø   User-defined functions are defined only once to perform a particular task but can be executed multiple times
                       in the same program.
                   Ø   A function call statement is given to execute the statements of a function.
                   Ø   Parameters of a function is the optional list of input value(s) required by the function to perform the required
                       task.
                   Ø   This list of values given during the function call is the argument(s) list.







                                                       Solved Exercises


                 A.  Multiple Choice Questions.
                    1.  Which of the following is the correct syntax for using the function sqrt() from math module to find the square root of 25?
                      a.  math import
                        sqrt(25)
                      b.  import math
                        sqrt(25)
                      c.  import math
                        math.sqrt(25)
                      d.  math import
                        math.sqrt(25)

                                                                                                           Modules    239
   248   249   250   251   252   253   254   255   256   257   258