Page 285 - Computer Science Class 11 With Functions
P. 285

11.2 User-defined Functions Revisited


            Program 11.1 Write a function interest(principal,rate,time) that compute simple interest.
            Let us reconsider the interest() function, developed to compute simple interest for the given principal amount,
            rate of interest per cent per annum, and time in years (Program 11.1).

              01 def interest(principal, rate, time):
              02     '''
              03     Objective: To determine simple interest
              04     Input Parameters:
              05     principal- numeric value denoting principal amount
              06       rate - numeric value denoting rate of interest in % per annum
              07       time - numeric values indicating time period in years
              08     Return value: float-simple interest
              09     '''
              10     simpleInterest = (principal*rate*time)/100
              11     return simpleInterest
            11.2.1 Default Parameters
            So far, we have learnt that a function call must have the same number of arguments as defined in the function
            definition. Let us consider the function interest() again, but this time we imagine a  scenario where the interest
            rate is 4% and the interest is usually computed for one year. In such a scenario, it would make sense to initialise the
            values of the formal parameters rate and time to 4 and 1, respectively, while defining the function interest().
            The formal parameters (rate and time in case of interest()) whose values are initialised as part of the function
            definition are called default parameters. The values assigned to the formal parameters are called the default values
            of the parameters. To define the function interest()with the default parameters, we only need to modify the
            function header (line 1) to specify the default values of the parameters as shown below:

              01 def interest(principal, rate = 4, time = 1):
              02     '''
              03     Objective: To determine simple interest
              04     Input Parameters:
              05      principal- numeric value denoting principal amount
              06      rate - numeric value denoting a rate of interest in % per annum
              07      time - numeric values indicating a time period in years
              08     Return value: float-simple interest
              09     '''
              10     simpleInterest = (principal*rate*time)/100
              11     return simpleInterest
            Now, to compute the simple interest for principal amount of Rs.55,000 for one year at 4% interest, we only need
            to invoke the function interest() with the argument 55000 and leave the other two parameters unspecified as
            shown below:
             >>> interest(55000)
                 2200.0
            Of course, a user may still specify the values of the default parameters. When a user specifies the values of the default
            parameters, the default values are ignored. Suppose we wish to compute the simple interest for the principal amount
            of Rs.55000 for one year with an interest rate of 5%.To carry out this computation, the following call to the function
            interest()takes the default value 1 for the parameter time:
             >>> interest(55000, 5)
                 2750.0
            We have already seen above that a function, such as interest(), may have more than one default parameters.
            The unspecified parameters in the trailing sequence take the default values. So, if a parameter takes a default value,
            then all the other parameters to its right must also take default values.



                                                                                                      Modules    283
   280   281   282   283   284   285   286   287   288   289   290