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

07 #Print a rhombus
              08 print("     *  ")
              09 print("    ***  ")
              10 print("   *****  ")
              11 print("  *******  ")
              12 print("   *****  ")
              13 print("    ***  ")
              14 print("     *  ")
              15 print()
              16 #Print a triangle
              17 print("     *  ")
              18 print("    ***  ")
              19 print("   *****  ")
              20 print("  *******  ")
            Onnexecutngnthenabovenprogram,nPythonnreopondonwithnthenfollowingnoutput:n

                 *
                ***
               *****
              *******


                 *
                ***
               *****
              *******
               *****
                ***
                 *


                 *
                ***
               *****
              *******
            Fig 8.1: Output on execution of Program 8.1n
            Thenabovenprogramnoufferonfromnthenfollowingntwondrawbacko:

            ● nAlthoughnthenprintngnthentrianglenandnrhombuonarenindependentnactviteo,nitnionnotnnnclearlynvioibleninnthenprogram.
            ● nThencodenfornprintngnthentrianglenappearontwice.n
            Wenalreadynknownthatnthenfunctononprovidenanoyotematcnapproachntonproblem-oolvingnbyndividingnancomplexnproblemn
            intonoimplernoub-problemo,ndevelopingnoolutononforneachnoub-problem,nandncombiningnthenindividualnoolutononofnthen
            oub-problemontonoolventhenoriginalnproblem.nSo,ntonovercomenthenabovendrawbacko,nwenwillndevelopnfunctononnamedn
            trianglenandnrhombusntonprintnantrianglenandnanrhombuo,nreopectvely.nAnPythonnprogramnionanglobalnframeninnwhichn
            functononandnotatementonappear.n
            Thenoyntaxnforndefiningnanfunctonnionaonfollowo:

            def function_name (comma_separted_dummy arguments):
                       Sequence of statements - Body of the function

             unctonndefinitonnbeginonwithnthenkeywordndef,nfollowednbynthennamenofnthenfuncton,nfollowednbynanpairnofnparentheoion
            thatnenclooeonanliotnofndummynargumenton(ifnany)noeparatednbyncommao.nDummynargumentonarenuoednaonnameonofn
            thenobjectonforndeocribingnthencomputatononinoidenanfuncton.nWhennanfunctonnioninvoked,nitondummynargumenton
            (alooncallednformalnparameteronorninputnparametero)narenreplacednbynactualnargumento.nThuo,ndummynargumentonactnaon
            placeholderonfornthenactualnargumento.n or,nexample,nconoidernthenbuilt-innfunctonnabs():n


                                                                                        Introductonnton unctono  183
   180   181   182   183   184   185   186   187   188   189   190