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

Corrected Code:

                          mynum = 9

                          def add9():
                              global mynum  # Declare mynum as a global variable
                              mynum = mynum + 9
                              print(mynum)


                          add9()  # Function call
                      (d)
                          1. Default parameter (val1 = 1.1) should be placed at the end of the parameter list.
                          2. The function name is defined as findValue, but it is called as findvalue (case-sensitive mismatch).

                          Corrected Code:

                          def findValue(val2, val3, val1=1.1):
                              final = (val2 + val3) / val1
                              print(final)

                          findValue()
                      (e)  Function call statement is written wrong.
                          It should be
                          message = greet()
                    2.  How is math.ceil(89.7) different from math.floor (89.7)?
                  Ans.  math.ceil(x) vs math.floor(x)
                      math.ceil(x): Return the ceiling of x, the smallest integer greater than or equal to x.
                      math.floor(x): Return the floor of x, the largest integer less than or equal to x.
                      For example:
                      >>>  import math
                      >>>  math.cell(25.369)
                      26
                      >>>  math.floor(25.369)
                      25
                    3.  Out of random() and randint(), which function should we use to generate random numbers between 1 and 5. Justify.
                  Ans.  randint() function of random module returns the number between start to end.
                      Example:
                      >>>  import random
                      >>>  random.randint(1,5)
                      4
                    4.  How is built-in function pow() function different from function math.pow() ? Explain with an example.
                  Ans.   pow() is an built-in function while math.pow() function defined under the math module. Both function is used to calculate x**y i.e.
                      x raised to power y.
                      When x and y are integer and given as argument to pow() and math.pow():
                      pow( x, y) function returns the value as integer type, while math.pow(x, y) function returns float type value.
                      For example:

                      >>>  print(type(pow(2,3)))
                      <class 'int'>
                      >>>  print(type(math.pow(2,3)))

                                                                                             Introduction to Functions  217
   226   227   228   229   230   231   232   233   234   235   236