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

9.2 User-defined Functions Revisited


               Program 9.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 9.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
              9.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.



               236   Touchpad Computer Science (Ver. 2.0)-XI
   245   246   247   248   249   250   251   252   253   254   255