-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.html
453 lines (404 loc) · 20.3 KB
/
Functions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PythonPythonME</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta content="Python,Programming" name="keywords" />
<meta content="Learn Python Programming" name="description" />
<meta name="generator" content="thesitewizard.com's Layout Wizard 3.0.1" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" media="all" href="styles.css" />
<style type="text/css">
.auto-style1 {
text-align: center;
}
.auto-style2 {
font-family: Georgia, "Times New Roman", Times, serif;
text-align: center;
}
.auto-style3 {
font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
text-align: center;
font-size: x-large;
}
.auto-style6 {
font-family: Georgia, "Times New Roman", Times, serif;
text-align: center;
font-size: large;
}
.auto-style4 {
font-family: "Times New Roman", Times, serif;
font-size: large;
}
.auto-style12 {
font-family: Georgia, "Times New Roman", Times, serif;
text-align: center;
font-size: large;
color: #1829BB;
}
.auto-style24 {
font-size: xx-large;
}
.auto-style25 {
font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
}
.auto-style26 {
font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
font-size: x-large;
}
.auto-style27 {
font-family: "Courier New", Courier, monospace;
}
.auto-style28 {
font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
text-align: center;
}
.auto-style29 {
font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
text-align: left;
}
.auto-style30 {
font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
text-align: left;
font-size: x-large;
}
.auto-style31 {
font-family: "Courier New", Courier, monospace;
text-align: left;
}
.auto-style32 {
font-size: x-large;
}
</style>
</head>
<body>
<div id="tswcontainer">
<div id="tswheader">
<!-- start of header section: insert your logo, etc, here -->
<p class="auto-style1">
<img alt="Logo" src="PythonPythonME.png" style="border-radius: 5px; width: 306px; height: 234px; background-color: #FFFFFF;" /></p>
<p class="auto-style3"><span lang="en-in"><strong>Functions</strong></span></p>
<!-- end of header section: insert your logo, etc, above this line -->
</div>
<div id="tswcontent">
<div id="tswcontproper">
<!-- start of content column: insert your content here -->
<p><span class="auto-style25">A function is a decided to name sequence of statements that computes
something. When you describe a function, you clearly state its name as
well as the order of its statements.</p>
<p>We've already seen one function call instance:</span></p>
<p>
<iframe src="https://trinket.io/embed/python3/9c352ac172?start=result&runMode=console" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
</p>
<p class="auto-style25">
The function's name is type. The expression enclosed in parentheses is
referred to as the function's <strong>argument</strong>. The type of the
argument is the result of this function.</p>
<p class="auto-style25">
A function is commonly said to "take" an argument and "return" a result.
The outcome is also known as the <strong>return value</strong>.</p>
<p class="auto-style25">
<span lang="en-in">We also have type casting included which involves
using functions as int(),str() etc to convert one data type values to
other.</span></p>
<p class="auto-style25">
<iframe src="https://trinket.io/embed/python3/d79a5e81dc?start=result&runMode=console" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
</p>
<p class="auto-style25">
<span lang="en-in">However while running int('Hello') error is produced
due to ValueError: invalid literal for int(): Hello</span></p>
<p class="auto-style26">
<strong>Math functions</strong></p>
<p class="auto-style25">
Python has a math module <span lang="en-in">which has </span>
mathematical functions. A module <span lang="en-in">usually </span>is a
file contain<span lang="en-in">ing</span> a collection of related
functions.</p>
<p class="auto-style27">
<span lang="en-in">>>> import math</span></p>
<p class="auto-style25">
Th<span lang="en-in">e above</span> statement generates
<span lang="en-in">a</span> module object<span lang="en-in"> called</span>
math. <span lang="en-in">Upon </span>display<span lang="en-in">ing</span>
module object, you get the following information about it:</p>
<p class="auto-style25">
<iframe src="https://trinket.io/embed/python3/e5c801d3df?start=result&runMode=console" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
</p>
<p class="auto-style25">
The module object contains the module's functions and variables. </p>
<p class="auto-style25">
To use one of the functions, enter the name of the module followed by
the name of the function, separated by a dot (also known as a period).
This is known as dot notation.</p>
<p class="auto-style25">
<iframe src="https://trinket.io/embed/python3/f8486f2cfc?start=result&runMode=console" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
</p>
<p class="auto-style25">
For instance, find the sine of radians. The variable name radians
implies that sin as well as the other trigonometric functions (cos, tan,
etc.) accept radian arguments. </p>
<p class="auto-style25">
Divide by 180 as well as multiply by to convert degrees to radians:</p>
<p class="auto-style25">
<iframe src="https://trinket.io/embed/python3/53b96fc0a9?start=result&runMode=console" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
</p>
<p class="auto-style26">
<strong>Composition</strong></p>
<p class="auto-style25">
One of most important functions of programming languages is one‘s
ability to take small basic components and compose them. </p>
<p class="auto-style25">
A function's argument, for example, can be any kind of expression, such
as arithmetic operators:</p>
<p class="auto-style25">
<iframe src="https://trinket.io/embed/python3/3e4ff9a46f?start=result&runMode=console" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
</p>
<p class="auto-style25">
<span lang="en-in">the composition of expression also has to follow a
specific order and a</span>ny expression on left is a syntax mistake (we
will see exceptions to this rule later).</p>
<p class="auto-style25">
<iframe src="https://trinket.io/embed/python3/3dbdbe59a5?start=result&runMode=console" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
</p>
<p class="auto-style25">
</p>
<p class="auto-style26">
<strong>Adding new functions</strong></p>
<p class="auto-style25">
A function definition stipulates the title of a new function as well as
the sequence of statements that will be executed when the function is
invoked.<br />
Here's an illustration:</p>
<p class="auto-style25">
<iframe src="https://trinket.io/embed/python3/bcf320de1b?start=result" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
</p>
<p class="auto-style25">
The keyword <strong>'def'</strong> indicates this is a function
definition. The function's name is print lyrics. </p>
<p class="auto-style25">
The same rules apply to function names as they do to variable names:<strong>
letters, numbers, and underscores</strong> are permitted, but the first
character cannot be a number.</p>
<p class="auto-style25">
A keyword cannot be used as the name of a function, and a variable and a
function with the same name should be avoided. </p>
<p class="auto-style25">
The absence of parentheses after the function name indicates that it
does not accept any arguments. </p>
<p class="auto-style25">
The first line of the function is referred to as the <strong>header</strong>,
and the remaining lines are referred to as the body.</p>
<p class="auto-style25">
The header must be followed by a colon, and the body must be indented. </p>
<p class="auto-style25">
<strong>Indentation</strong> is always four spaces by convention. The
body may contain an unlimited number of statements.</p>
<p class="auto-style28">
<img alt="Indentation" src="images/Screenshot%202022-03-26%20175733.png" style="width: 515px; height: 127px" /></p>
<p class="auto-style30">
<strong>Definitions and uses</strong></p>
<p class="auto-style29">
Putting the code blocks from the preceding part together, the entire
program looks like this:</p>
<p class="auto-style28">
<img alt="Definitiona" src="images/Screenshot%202022-03-26%20180107.png" style="width: 532px; height: 275px" /></p>
<p class="auto-style29">
There are two function definitions in this program: <em>print lyrics</em>
and <em>repeat lyrics</em>. </p>
<p class="auto-style29">
<strong>Function definitions</strong> are executed in the same way as
other statements, but the result is the creation of function objects. </p>
<p class="auto-style29">
The statements within the function are not executed until the function
is invoked, and the function definition produces no output.</p>
<p class="auto-style29">
Before you can run a function, you must first create it. In those other
words, the function definition must be executed before the function is
invoked.</p>
<p class="auto-style30">
<strong>Flow of execution</strong></p>
<p class="auto-style29">
The program's execution always starts with the first statement. </p>
<p class="auto-style29">
Statements are executed one at a time, from top to bottom.</p>
<p class="auto-style29">
Function definitions have no effect on the program's execution flow, but
keep in mind that statements inside <em>the function are not executed
until the function is called</em>.</p>
<p class="auto-style29">
A function call is similar to an excursion in the execution flow. </p>
<p class="auto-style29">
Instead of proceeding to the next statement, the <em>flow leaps</em> to
the function's body, runs the statements there, and afterwards returns
to pick up where it left off.</p>
<p class="auto-style30">
<strong>Parameters and arguments</strong></p>
<p class="auto-style29">
Some of the functions we've seen so far have required arguments.</p>
<p class="auto-style29">
When you call math.sin, for example, you pass a number as an <em>
argument</em>. </p>
<p class="auto-style29">
Some functions accept <em>multiple arguments</em>: math.pow accepts two,
<strong>the base</strong> and <strong>the exponent</strong>.</p>
<p class="auto-style28">
<img alt="parameters1" src="images/Screenshot%202022-03-26%20182504.png" style="width: 381px; height: 126px" /><img alt="parameters" src="images/Screenshot%202022-03-26%20182521.png" style="width: 425px; height: 161px" /></p>
<p class="auto-style29">
<span lang="en-in">Here the function is<strong> 'print_twice'</strong>
as mentioned here. This function assigns the argument to a parameter
named name. </span></p>
<p class="auto-style29">
<span lang="en-in">When the function is called, it prints the value
assigned to name twice as output.</span></p>
<p class="auto-style29">
The same composition rules apply to programmer-defined functions as they
do to built-in functions.</p>
<p class="auto-style29">
<iframe src="https://trinket.io/embed/python3/b146d6647b?start=result&runMode=console" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
<span lang="en-in">Here the </span>argument is evaluated before
the function is called<span lang="en-in"> as seen from above execution.</span></p>
<p class="auto-style30">
<strong>Variables and parameters are local</strong></p>
<p class="auto-style29">
When you create a variable inside a function, it is local, meaning it
lives only within the function.</p>
<p class="auto-style29">
<iframe src="https://trinket.io/embed/python3/17fc5b537e?start=result&runMode=console" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
The variable cat is destroyed when function printing twice
finishes execution. When we try to print it, we receive the following
error:</p>
<p class="auto-style31">
NameError: name 'cat' is not defined</p>
<p class="auto-style30">
<strong>Stack diagrams</strong></p>
<p class="auto-style29">
A <strong>stack diagram </strong>might be handy for keeping track of
which variables can be used where. Stack diagrams, like state diagrams,
indicate the <em>value of each variable</em> as well as <em>the function
to which each variable belongs</em>.</p>
<p class="auto-style29">
A <strong>frame</strong> is used to represent each function. A frame is
a box that contains the arguments and variables of a function as well as
the name of the function.</p>
<p class="auto-style29">
The frames are stacked to show which function called which function, and
so on. <span lang="en-in">In our example, the function
<span class="auto-style27">'p</span></span><span class="auto-style27">rint<span lang="en-in">_</span>twice<span lang="en-in">'</span></span>
was called by <span lang="en-in">the function <span class="auto-style27">
'</span></span><span class="auto-style27">printing<span lang="en-in">_</span>twice</span><span lang="en-in">'</span>,
which was called by <span class="auto-style27">__main__<span lang="en-in">.</span></span></p>
<p class="auto-style29">
<span lang="en-in"><span class="auto-style27">__main__ </span>is a</span>
special name for the topmost frame in this case. <span lang="en-in">Upon
creating a variable outside a functon i</span>t belongs to
<span class="auto-style27">__main__<span lang="en-in">.</span></span></p>
<p class="auto-style29">
A <strong>traceback</strong> is a collection of functions. It
shows you the software file the error occurred in, as well as which line
and functions were active at the moment. </p>
<p class="auto-style28">
<img alt="Traceback" src="images/New%20Project_new.png" style="width: 596px; height: 403px" /></p>
<p class="auto-style29">
It also displays the error-causing line of code.<span lang="en-in">
</span>The order of the functions in the traceback corresponds to the
stack diagram's frame order. </p>
<p class="auto-style29">
<span lang="en-in">Our name error trace back would look like :</span></p>
<p class="auto-style29">
<img alt="Traceback" src="images/Screenshot%202022-03-30%20171528.png" style="width: 1027px; height: 143px" /></p>
<p class="auto-style30">
<strong>Fruitful functions and void functions</strong></p>
<p class="auto-style29">
Fruitful functions are functions that return outcomes in the form of
math functions. While void functions are functions that perform an
action but do not return a value, such as print twice. </p>
<p class="auto-style29">
<iframe src="https://trinket.io/embed/python3/3c4064a8cc?start=result&runMode=console" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
</p>
<p class="auto-style29">
You nearly always want to do something with the result when you call a
productive function. </p>
<p class="auto-style29">
If you call a productive function by itself in a script, the return
value is lost permanently. </p>
<p class="auto-style29">
This script computes the square root of 5, however it isn't particularly
useful because it doesn't store or display the result.</p>
<p class="auto-style28">
<img alt="Sqrt" src="images/Screenshot%202022-03-30%20172530.png" style="width: 197px; height: 62px" /></p>
<p class="auto-style29">
There is <strong>no return value</strong> for <strong>void functions</strong>,
therefore they may display something on the screen or have some other
effect. You obtain a special value called None if you assign the result
to a variable.</p>
<p class="auto-style29">
The value None is not the same as the string 'None'. It is a special
value that has its own type:</p>
<p class="auto-style28">
<img alt="None" src="images/Screenshot%202022-03-30%20172905.png" style="width: 202px; height: 38px" /></p>
<p class="auto-style29">
<span class="auto-style32" lang="en-in"><strong>Why use Functions ?</strong></span></p>
<p class="auto-style29">
<span lang="en-in">Use Case of functions are essential as it allows to
break a larger complex program imto smaller and more easily manageable
parts.</span></p>
<p class="auto-style29">
<span lang="en-in">Some of the advantages are listed below :</span></p>
<ul>
<li>
<p class="auto-style29">When you create a new function, you can name
a group of statements, making your software easier to read and
debug.</p>
</li>
<li>
<p class="auto-style29">Functions can reduce the size of a program
by removing redundant code. If you need to make a modification
later, you just have to do it once.</p>
</li>
<li>
<p class="auto-style29">Breaking down a large program into functions
allows you to debug each component individually before putting them
together into a workable whole.</p>
</li>
<li>
<p class="auto-style29">Many programs benefit from well-designed
functionalities. You can reuse one once you've written and debugged
it.</p>
</li>
</ul>
<p class="auto-style29">
</p>
<!-- end of content column: put your content above this line -->
</div>
</div>
<div id="tswsidecol">
<div id="tswsideproper">
<p class="auto-style12">
<strong><span class="auto-style24">Content</span></strong>
</p>
<p class="auto-style6">
<a href="index.html">PythonPythonME</a></p>
<p class="auto-style6"><span lang="en-in">
<a href="Running%20Python.html">Running Python</a></span></p>
<p class="auto-style6"><span lang="en-in"><a href="Variables.html">
Variables</a></span></p>
<p class="auto-style6"><span lang="en-in"><a href="Functions.html">
Functions</a></span></p>
<p class="auto-style6"><span lang="en-in"><a href="Terms_1.html">Terms</a></span></p>
<!-- end of side column: put your navigation menu, etc, above this line -->
</div>
</div>
<div style="clear: both;" class="auto-style1"><br />
<span lang="en-in">
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------</span><br />
<img alt="Footer " height="64" src="images/star-line-divider-design-footer-modern-border-vector-23980591.png" width="1118" /><br />
<span lang="en-in">
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------</span><br />
</div>
</div>
<p class="auto-style2">Copyright
<span class="auto-style4" style="color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">
© 2022-2023. Anoop Johny. </span>All Rights Reserved.</p>
</body>
</html>