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

● nLinen1ndefineonthenfunctonnheadernfornthenfunctonninterest().n
            ● nNotenthatnthenfunctonndefinitonnbeginonwithnthenkeywordndef,nfollowednbynthennamenofnthenfuncton-intereot,n
              followednbynthendummynargumenton(tonbenuoednforncomputaton)nwithinnparentheoio,nandnfollowednbynancolonnatnthen
              end.

            ● nLineon2-11,ncomprioenanoequencenofnotatementonformingnthenfuncton'onbody.n
            ● nLineon2-9nopannanmult-linenotringn(callednandocotring).nThendocotringnionfollowednbynthenotatementnthatncomputeon
              oimplenintereotn(linen10).n
            ● n inally,ntherenionanreturnnotatementnthatnreturnonthenreoultnofnthencomputatonntonthenotatementnthatninvokednthen
              functon.

            Program 8.3a Computatonnofnoimplenintereot

              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
            Now,nwenwillnmakenuoenofnthenabovendefinitonnofnthenfunctonninterest()ntoncomputenoimplennintereotnfornanopecifiedn
            amount,nintereotnrate,nandntmen(Programn8.3b,nlineon22-27).

            Program 8.3b Computatonnofnoimplenintereot

              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
              12
              13 #main program segment
              14 '''
              15 Objective: To compute simple interest
              16 User Interface:
              17     1. User is asked to enter:
              18          principal amount, rate of interest, time period of interest
              19     2. Output to the user:
              20          Simple interest computed
              21 '''
              22 p = int(input('Enter the principal amount: '))
              23 r = int(input('Enter the rate of interest: '))
              24 t = int(input('Enter the time period: '))
              25 assert p>=0 and r>=0 and t>=0
              26 simpleInterest = interest(p, r, t)
              27 print('Simple Interest:', simpleInterest)



                                                                                        Introductonnton unctono  189
   186   187   188   189   190   191   192   193   194   195   196