Page 186 - Computer Science Class 11 With Functions
P. 186
>>> help(abs)
Help on built-in function abs in module builtins:
abs(x, /)
Return the absolute value of the argument.
>>> abs(-24)
24
Whennweninvokednthenfuncton abs()nwithnthenactualnargument -24,nthendummynargumentnxn(uoedninoidenthen
functon abs()nforncomputngnthenaboolutenvalue)nwaonreplacednbynthennargumentn-24.
Thenruleonfornnamingnanfunctonnarenthenoamenaonthooenfornnamingnannidentfier.nRecallnthatnnamenofnannidentfiernohouldn
notnbenanPythonnkeyword.nThenfirotnlinenofnanfunctonndefinitonnthatnendonwithnancolonnionknownnaonthenfunctonnheader.n
Thenoequencenofnotatementonfollowednbynthencolonnionrightnindentednandnformonthenfuncton'onbodyn(alooncallednfunctonn
block).nThenoequencenofnotatementonincludedninnanfuncton'onbodynionexecutednwhennitnioninvoked.
1. What is wrong with the following function header?
Def test():
2. What is wrong with the following function header?
def test()
3. What is wrong with the following function definition?
def test():
print("Hello")
Nownwenarenreadyntondefinenthenfunctonontriangle() andnnrhombus() whichnformnpartnofnprogramn8.2a.n
● nInnLinen1,nthenfunctonnheadernfornthentriangle()niondefined.
● nNotenthatnthenfunctonndefinitonnbeginonwithnthenkeywordndef,nfollowednbynthennamenofnthenfunctonntriangle(),n
thenparentheoio,nandnancolonnatnthenendnofnthenline.nLineon02-10,ncomprioenanoequencenofnotatementonformingnthen
functon'onbody.n
● nThen Lineon 02-06,n opann an mult-linen otring(calledn an docotring)n enclooedn withinn triplen quoteon forn documentatonn
purpooeonandnarenignorednbynthenPythonninterpreter.n* urther,naonthentrianglenhaonanfixednotructure,nwendonnotn
requirenuoerninputo.n
● nThenLineon07-10,nprintnthentriangle.nSimilarly,ninnlineon07-10,nwendevelopnanothernuoer-definednfunctonnrhombus(),n
thatnprintonthenrhombuo.
Program 8.2a: Thenprogramndefineonfunctonontondioplaynantrianglenandnanrhombuo.
01 def triangle():
02 '''
03 Objective: To print the triangle
04 Input Parameters: None
05 Return value: None
06 '''
07 print(" * ")
08 print(" *** ")
09 print(" ***** ")
10 print(" ******* ")
11
12 def rhombus():
13 '''
14 Objective: To print the rhombus
184 Touchpad Computer Science-XI

