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

num = 25
                        def function():
                            # Global Variable-num
                            print("Number (Global variable) inside function:", num)
                            x = 10
                            # Local Variable-x
                            print("Local Variable x:", x)
                            result = sqrt(num) + x


                        print("Number outside function before call:", num)
                        function()
                        print("Number outside function after call:", num)
                    7.  Does a function always return a value? Explain with an example.
                  Ans.   Yes. When the return statement is explicitly used in a function, the function returns the result of the computation to the statement that
                      invoked the function. In absence of any explicit return statement, by default, the function returns None value. For example:
                      def areaCircle(radius):
                          area = 3.14 * radius ** 2
                          return area

                      returnedVal = areaCircle(5)
                      print("Value returned by areaCircle", returnedVal)

                      def pattern(n):
                          for i in range(1, n+1):
                              print("*" * i)

                      returnedVal = pattern(5)
                      print("Value returned by pattern", returnedVal)

                      Output:
                      Value returned by areaCircle 78.5
                      *
                      **
                      ***
                      ****
                      *****
                      Value returned by pattern None

                                                         ACTIVITY-BASED QUESTIONS
                       Note: Writing a program implies:
                      ● Adding comments as part of documentation
                       ● Writing function definition
                       ● Executing the function through a function call
                    1.  To secure your account, whether it be an email, online bank account or any other account, it is important that we use authentication. Use
                      your programming expertise to create a program using user defined function named login that accepts userid and password as
                      parameters (login(uid,pwd)) that displays a message "account blocked" in case of three wrong attempts. The login is successful if
                      the user enters user ID as "ADMIN" and password as "St0rE@1". On successful login, display a message "login successful".




                                                                                             Introduction to Functions  219
   228   229   230   231   232   233   234   235   236   237   238