Repository navigation
Expand file tree
/
Copy path6_Python_functions.html
More file actions
707 lines (623 loc) · 53.7 KB
/
Copy path6_Python_functions.html
File metadata and controls
707 lines (623 loc) · 53.7 KB
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Functions in Python: A Complete Guide · Python Programming Series</title>
<meta name="description" content="A complete guide to Python functions: definition vs call, parameters vs arguments, positional/keyword/default/arbitrary arguments, return vs print, and scope, with common mistakes to watch for.">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@500;600;700&family=IBM+Plex+Sans:ital,wght@0,400;0,500;0,600;1,400&family=IBM+Plex+Mono:wght@400;500&display=swap" rel="stylesheet">
<style>:root{
--bg:#EEF6F6;--bg-dot:rgba(10,110,120,.06);--surface:#fff;--surface-soft:#F4FAFA;
--ink:#0A1E1F;--ink-soft:#33504E;--ink-faint:#7FA8A5;
--teal:#0E8A96;--teal-text:#0A6F79;--teal-soft:rgba(14,138,150,.1);
--coral:#DC6B3D;--coral-text:#B85630;--coral-soft:rgba(220,107,61,.12);
--violet:#7A52B8;--violet-text:#63409A;--violet-soft:rgba(122,82,184,.1);
--line:#D4E5E4;--line-strong:#AFCFCD;
--code-bg:#08201F;--code-text:#DCEEEC;--code-line:#123331;
--r-s:6px;--r-m:10px;--r-l:16px;
--shadow:0 1px 3px rgba(10,30,31,.06),0 8px 28px -12px rgba(10,30,31,.16);
--font-d:'Space Grotesk','Arial Narrow',sans-serif;
--font-b:'IBM Plex Sans',-apple-system,'Segoe UI',sans-serif;
--font-m:'IBM Plex Mono','SFMono-Regular',Consolas,monospace;
}
*,*::before,*::after{box-sizing:border-box;}
html{scroll-behavior:smooth;}
body{
margin:0;
background:
linear-gradient(var(--bg-dot) 1px, transparent 1px) 0 0/26px 26px,
linear-gradient(90deg, var(--bg-dot) 1px, transparent 1px) 0 0/26px 26px,
var(--bg);
color:var(--ink);font-family:var(--font-b);font-size:16.5px;line-height:1.65;-webkit-font-smoothing:antialiased;
}
a{color:var(--teal-text);text-decoration-color:rgba(10,111,121,.32);text-underline-offset:2px;}
a:hover{text-decoration-color:currentColor;}
::selection{background:var(--teal);color:#fff;}
:focus-visible{outline:2.5px solid var(--teal);outline-offset:2px;border-radius:3px;}
.icon{width:1em;height:1em;display:inline-block;vertical-align:-.15em;fill:none;stroke:currentColor;stroke-width:1.6;stroke-linecap:round;stroke-linejoin:round;}
.shell{max-width:1300px;margin:0 auto;padding:0 clamp(16px,4vw,44px);}
.topbar{display:none;position:sticky;top:0;z-index:40;align-items:center;justify-content:space-between;padding:12px clamp(16px,4vw,44px);background:rgba(238,246,246,.92);backdrop-filter:blur(8px);border-bottom:1px solid var(--line);}
.topbar .brand{font-family:var(--font-d);font-weight:700;font-size:.93rem;}
.topbar .brand span{color:var(--teal);}
.topbar button{border:1.5px solid var(--line-strong);background:var(--surface);border-radius:var(--r-s);width:38px;height:34px;display:flex;align-items:center;justify-content:center;cursor:pointer;color:var(--ink);}
.hero{padding:clamp(48px,7vw,88px) 0 clamp(32px,5vw,52px);}
.hero-eyebrow{font-family:var(--font-m);font-size:.77rem;letter-spacing:.14em;text-transform:uppercase;color:var(--teal-text);font-weight:500;display:flex;align-items:center;gap:.6em;margin-bottom:18px;}
.hero-eyebrow::before{content:"";width:26px;height:2px;background:var(--teal);display:inline-block;}
h1.title{font-family:var(--font-d);font-weight:700;font-size:clamp(2rem,4.2vw,3.2rem);line-height:1.1;letter-spacing:-.02em;margin:0 0 18px;max-width:23ch;}
h1.title em{font-style:normal;color:var(--teal-text);}
.hero-sub{font-size:clamp(1rem,1.55vw,1.16rem);color:var(--ink-soft);max-width:66ch;margin:0 0 30px;}
.hero-chips{display:flex;flex-wrap:wrap;gap:9px;}
.chip{display:flex;align-items:center;gap:8px;background:var(--surface);border:1px solid var(--line);padding:8px 14px 8px 10px;border-radius:999px;font-family:var(--font-m);font-size:.76rem;color:var(--ink-soft);box-shadow:var(--shadow);}
.chip .icon{width:1.1em;height:1.1em;}
.chip:nth-child(1) .icon{color:var(--teal);}
.chip:nth-child(2) .icon{color:var(--coral);}
.chip:nth-child(3) .icon{color:var(--violet);}
.chip:nth-child(4) .icon{color:var(--teal);}
.layout{display:grid;grid-template-columns:270px minmax(0,1fr);gap:clamp(24px,4vw,52px);align-items:start;padding-bottom:100px;}
.sidebar{position:sticky;top:28px;max-height:calc(100vh - 56px);overflow-y:auto;padding-right:6px;}
.sidebar::-webkit-scrollbar{width:5px;}
.sidebar::-webkit-scrollbar-thumb{background:var(--line-strong);border-radius:3px;}
.toc-label{font-family:var(--font-m);font-size:.71rem;letter-spacing:.12em;text-transform:uppercase;color:var(--ink-faint);margin:4px 0 10px;padding-left:14px;}
.toc{list-style:none;margin:0 0 16px;padding:0;border-left:1.5px solid var(--line);}
.toc li{margin:0;}
.toc a{display:flex;align-items:center;gap:9px;padding:6px 14px;margin-left:-1.5px;padding-left:12.5px;border-left:1.5px solid transparent;font-size:.83rem;color:var(--ink-soft);text-decoration:none;transition:all .14s;border-radius:0 var(--r-s) var(--r-s) 0;}
.toc a .icon{width:1em;height:1em;flex:none;color:var(--ink-faint);transition:color .14s;}
.toc a:hover{color:var(--ink);background:var(--surface-soft);}
.toc a.active{color:var(--teal-text);border-left-color:var(--teal);background:var(--teal-soft);font-weight:500;}
.toc a.active .icon{color:var(--teal);}
.article{min-width:0;padding-top:10px;}
.article h2{font-family:var(--font-d);font-weight:600;font-size:clamp(1.45rem,2.4vw,1.82rem);letter-spacing:-.01em;margin:58px 0 16px;padding-top:8px;scroll-margin-top:22px;display:flex;align-items:center;gap:12px;}
.article>h2:first-of-type{margin-top:6px;}
.article h2 .h-icon{width:1.4rem;height:1.4rem;flex:none;color:var(--teal);}
.article h2 .h-icon .icon{width:100%;height:100%;}
.article h3{font-family:var(--font-d);font-weight:600;font-size:1.08rem;margin:28px 0 10px;scroll-margin-top:22px;}
.article p{margin:0 0 16px;}
.article ul,.article ol{margin:0 0 18px;padding-left:1.3em;}
.article li{margin-bottom:7px;}
.article li::marker{color:var(--teal);}
.article strong{font-weight:600;}
.article hr{border:none;border-top:1px solid var(--line);margin:44px 0;}
.article code{font-family:var(--font-m);font-size:.85em;background:var(--surface-soft);border:1px solid var(--line);padding:.1em .36em;border-radius:4px;color:var(--violet-text);}
.article blockquote{margin:20px 0;padding:14px 18px;border-left:3px solid var(--coral);background:var(--coral-soft);border-radius:0 var(--r-s) var(--r-s) 0;color:var(--ink-soft);}
.article blockquote p{margin:0 0 8px;color:inherit;}
.article blockquote p:last-child{margin-bottom:0;}
.table-wrap{overflow-x:auto;margin:20px 0 26px;border-radius:var(--r-m);box-shadow:var(--shadow);}
.article table{width:100%;border-collapse:collapse;background:var(--surface);font-size:.88rem;}
.article thead th{background:var(--teal);color:#fff;font-family:var(--font-d);font-weight:600;text-align:left;padding:11px 15px;white-space:nowrap;font-size:.8rem;letter-spacing:.01em;}
.article tbody td{padding:10px 15px;border-bottom:1px solid var(--line);vertical-align:top;}
.article tbody tr:last-child td{border-bottom:none;}
.article tbody tr:nth-child(even){background:var(--surface-soft);}
.article tbody td code{white-space:nowrap;}
.code-card{margin:20px 0 24px;border-radius:var(--r-m);overflow:hidden;background:var(--code-bg);box-shadow:var(--shadow);}
.code-card .code-head{display:flex;align-items:center;justify-content:space-between;padding:9px 14px;background:#061916;border-bottom:1px solid var(--code-line);}
.code-card .dots{display:flex;gap:5px;}
.code-card .dots span{width:8px;height:8px;border-radius:50%;display:block;}
.code-card .dots span:nth-child(1){background:#E8664A;}
.code-card .dots span:nth-child(2){background:#E3B23C;}
.code-card .dots span:nth-child(3){background:#3CB371;}
.code-card .lang{font-family:var(--font-m);font-size:.7rem;color:#5A9A92;letter-spacing:.06em;}
.code-card .copy-btn{font-family:var(--font-m);font-size:.7rem;color:#7AB0A8;background:transparent;border:1px solid #1A4A44;border-radius:5px;padding:3px 9px;cursor:pointer;display:flex;align-items:center;gap:5px;transition:all .14s;}
.code-card .copy-btn:hover{background:#0F3530;color:#fff;border-color:#2F6A60;}
.code-card .copy-btn .icon{width:.9em;height:.9em;}
.code-card pre{margin:0;padding:14px 18px;overflow-x:auto;}
.code-card code{font-family:var(--font-m);font-size:.855rem;line-height:1.6;color:var(--code-text);background:none;border:none;padding:0;}
.code-card .k,.code-card .kn,.code-card .kd{color:#7FD4C8;}
.code-card .nb{color:#7FD4C8;}
.code-card .nf,.code-card .fm{color:#E3A2C0;}
.code-card .s,.code-card .s1,.code-card .s2,.code-card .sa{color:#D8C878;}
.code-card .mi,.code-card .mf{color:#F0A25C;}
.code-card .o,.code-card .p{color:#5A9A92;}
.code-card .c,.code-card .c1,.code-card .cm{color:#3E6E68;font-style:italic;}
.code-card .n{color:var(--code-text);}
.code-card .nc{color:#F0D060;}
.output-card{margin:-14px 0 24px;border-radius:0 0 var(--r-m) var(--r-m);overflow:hidden;background:#04120F;border-top:1px dashed #1A4A44;}
.output-card::before{content:"OUTPUT";display:block;font-family:var(--font-m);font-size:.62rem;letter-spacing:.14em;color:#4E7A72;padding:8px 18px 0;}
.output-card pre{margin:0;padding:6px 18px 16px;overflow-x:auto;}
.output-card code{font-family:var(--font-m);font-size:.82rem;line-height:1.55;color:#A0C8C0;background:none;border:none;padding:0;}
.diagram-card{margin:20px 0 24px;border-radius:var(--r-m);overflow:hidden;position:relative;border:1.5px dashed var(--line-strong);background:
linear-gradient(var(--bg-dot) 1px, transparent 1px) 0 0/20px 20px,
linear-gradient(90deg, var(--bg-dot) 1px, transparent 1px) 0 0/20px 20px,
var(--surface-soft);}
.diagram-card::before{content:"diagram";position:absolute;top:9px;right:12px;font-family:var(--font-m);font-size:.64rem;letter-spacing:.14em;text-transform:uppercase;color:var(--ink-faint);}
.diagram-card pre{margin:0;padding:24px 20px 18px;overflow-x:auto;}
.diagram-card code{font-family:var(--font-m);font-size:.8rem;line-height:1.55;color:var(--ink-soft);background:none;border:none;padding:0;white-space:pre;}
.article-end{margin-top:52px;padding:26px 24px;border-radius:var(--r-m);background:var(--surface);border:1px solid var(--line);box-shadow:var(--shadow);}
.article-end p{margin:0;font-size:1rem;color:var(--ink-soft);}
.article-end p strong{color:var(--ink);}
footer.site-footer{border-top:1px solid var(--line);margin-top:36px;padding:22px clamp(16px,4vw,44px) 40px;display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:10px;}
footer.site-footer .f-left{font-family:var(--font-m);font-size:.76rem;color:var(--ink-faint);}
footer.site-footer a{font-size:.8rem;color:var(--ink-soft);}
@media(max-width:960px){
.topbar{display:flex;}
.layout{grid-template-columns:1fr;}
.sidebar{position:fixed;inset:57px 0 0 0;z-index:35;background:var(--bg);padding:16px clamp(16px,4vw,44px) 24px;transform:translateX(-100%);transition:transform .2s;max-height:none;height:calc(100vh - 57px);}
.sidebar.open{transform:translateX(0);}
}
@media(prefers-reduced-motion:reduce){html{scroll-behavior:auto;}.sidebar{transition:none;}}
.series-nav{display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-top:28px;}
.series-nav a{display:block;padding:15px 18px;border:1px solid var(--line);border-radius:var(--r-m);background:var(--surface);box-shadow:var(--shadow);text-decoration:none;transition:border-color .14s;}
.series-nav a:hover{border-color:var(--indigo);}
.series-nav .dir{display:block;font-family:var(--font-m);font-size:.68rem;letter-spacing:.12em;text-transform:uppercase;color:var(--ink-faint);margin-bottom:5px;}
.series-nav .name{display:block;font-family:var(--font-d);font-weight:600;font-size:.97rem;color:var(--ink);line-height:1.3;}
.series-nav .next{grid-column:2;text-align:right;}
.series-nav .all{grid-column:1/-1;text-align:center;border:none;background:none;box-shadow:none;padding:4px;font-family:var(--font-m);font-size:.72rem;letter-spacing:.1em;text-transform:uppercase;color:var(--ink-soft);}
.series-nav .all:hover{border:none;color:var(--indigo-text);}
.topbar a.brand{text-decoration:none;color:var(--ink);}
@media(max-width:560px){.series-nav{grid-template-columns:1fr;}.series-nav .next{grid-column:1;text-align:left;}}
</style>
</head>
<body>
<svg style="display:none" aria-hidden="true"><defs>
<symbol id="icon-machine" viewBox="0 0 24 24">
<rect x="6" y="5" width="12" height="14" rx="2"/>
<line x1="1" y1="9" x2="6" y2="9"/><line x1="1" y1="15" x2="6" y2="15"/>
<line x1="18" y1="12" x2="23" y2="12"/>
<polyline points="21,9.5 23,12 21,14.5"/>
</symbol>
<symbol id="icon-anatomy" viewBox="0 0 24 24">
<rect x="2" y="3" width="20" height="6" rx="1.3" fill="currentColor" stroke="none" opacity=".2"/>
<rect x="2" y="3" width="20" height="6" rx="1.3"/>
<line x1="4" y1="13" x2="20" y2="13"/>
<line x1="4" y1="17" x2="20" y2="17"/>
<line x1="4" y1="21" x2="14" y2="21"/>
</symbol>
<symbol id="icon-swap" viewBox="0 0 24 24">
<rect x="2" y="4" width="8" height="6" rx="1.3"/>
<rect x="14" y="4" width="8" height="6" rx="1.3" fill="currentColor" stroke="none" opacity=".6"/>
<path d="M12 14 L12 20"/><polyline points="9,17 12,20 15,17"/>
</symbol>
<symbol id="icon-positional" viewBox="0 0 24 24">
<rect x="2" y="9" width="5" height="6" rx="1" fill="currentColor" stroke="none"/>
<rect x="9.5" y="9" width="5" height="6" rx="1" opacity=".55" fill="currentColor" stroke="none"/>
<rect x="17" y="9" width="5" height="6" rx="1" opacity=".3" fill="currentColor" stroke="none"/>
<path d="M2 6 L22 6" stroke-dasharray="2 2"/>
<polyline points="19,3.5 22,6 19,8.5"/>
</symbol>
<symbol id="icon-keyword" viewBox="0 0 24 24">
<path d="M3 12 L9 6 L21 6 L21 12 L21 18 L9 18 Z"/>
<circle cx="7" cy="12" r="1.3" fill="currentColor" stroke="none"/>
</symbol>
<symbol id="icon-default" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="9.3"/>
<path d="M8 12.5 L10.8 15.3 L16 9" stroke-width="1.7"/>
</symbol>
<symbol id="icon-star" viewBox="0 0 24 24">
<line x1="12" y1="3" x2="12" y2="21"/>
<line x1="4.5" y1="7.5" x2="19.5" y2="16.5"/>
<line x1="19.5" y1="7.5" x2="4.5" y2="16.5"/>
</symbol>
<symbol id="icon-builtin" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="3"/>
<path d="M12 3v2M12 19v2M4.2 4.2l1.4 1.4M18.4 18.4l1.4 1.4M3 12h2M19 12h2M4.2 19.8l1.4-1.4M18.4 5.6l1.4-1.4"/>
</symbol>
<symbol id="icon-return" viewBox="0 0 24 24">
<path d="M8 6 L3 12 L8 18"/>
<path d="M3 12 L17 12 A4 4 0 0 0 17 4 L13 4"/>
</symbol>
<symbol id="icon-scope" viewBox="0 0 24 24">
<rect x="8" y="8" width="13" height="13" rx="1.5"/>
<rect x="2" y="3" width="13" height="5" rx="1.3" opacity=".5"/>
<circle cx="14.5" cy="14.5" r="2.3" fill="currentColor" stroke="none"/>
</symbol>
<symbol id="icon-world" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="9.3"/>
<ellipse cx="12" cy="12" rx="4" ry="9.3"/>
<line x1="2.7" y1="12" x2="21.3" y2="12"/>
</symbol>
<symbol id="icon-table" viewBox="0 0 24 24">
<rect x="2" y="3" width="20" height="18" rx="1.5"/>
<line x1="2" y1="9" x2="22" y2="9"/>
<line x1="9" y1="3" x2="9" y2="21"/>
<line x1="15.5" y1="3" x2="15.5" y2="21"/>
</symbol>
<symbol id="icon-copy" viewBox="0 0 24 24">
<rect x="8" y="8" width="12" height="13" rx="1.6"/>
<path d="M5 16H4.5A1.5 1.5 0 0 1 3 14.5v-10A1.5 1.5 0 0 1 4.5 3h10A1.5 1.5 0 0 1 16 4.5V5"/>
</symbol>
<symbol id="icon-check" viewBox="0 0 24 24">
<path d="M4 12.5 L9.5 18 L20 5"/>
</symbol>
<symbol id="icon-menu" viewBox="0 0 24 24">
<line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="18" x2="21" y2="18"/>
</symbol>
<symbol id="icon-close" viewBox="0 0 24 24">
<line x1="5" y1="5" x2="19" y2="19"/><line x1="19" y1="5" x2="5" y2="19"/>
</symbol>
</defs></svg>
<div class="topbar">
<a class="brand" href="index.html">§6 <span>Functions</span></a>
<button id="navToggle" aria-label="Toggle navigation">
<svg class="icon"><use href="#icon-menu"></use></svg>
</button>
</div>
<div class="shell">
<section class="hero">
<div class="hero-eyebrow">Python Programming Series · Article 6</div>
<h1 class="title">Functions in Python: A Complete Guide</h1>
<p class="hero-sub">A function is a named, reusable block of code: write the logic once, then run it as many times as you need with <code>function_name()</code>. Without functions, any piece of logic you need more than once has to be copy-pasted everywhere it's used, and every one of those copies needs to be found and fixed if the logic ever changes. Functions are the fix.</p>
<div class="hero-chips">
<div class="chip"><svg class="icon"><use href="#icon-anatomy"></use></svg> Full anatomy of a function</div>
<div class="chip"><svg class="icon"><use href="#icon-positional"></use></svg> 4 argument types</div>
<div class="chip"><svg class="icon"><use href="#icon-check"></use></svg> Common mistakes to watch for</div>
<div class="chip"><svg class="icon"><use href="#icon-world"></use></svg> Real code from YouTube, Amazon, Uber</div>
</div>
</section>
<div class="layout">
<nav class="sidebar" id="sidebar">
<div class="toc-label">On this page</div>
<ul class="toc">
<li><a href="#the-anatomy-of-a-function"><svg class="icon"><use href="#icon-anatomy"></use></svg><span>The Anatomy of a Function</span></a></li>
<li><a href="#parameters-vs-arguments"><svg class="icon"><use href="#icon-swap"></use></svg><span>Parameters vs. Arguments</span></a></li>
<li><a href="#types-of-arguments"><svg class="icon"><use href="#icon-positional"></use></svg><span>Types of Arguments</span></a></li>
<li><a href="#built-in-functions-vs-user-defined-functions"><svg class="icon"><use href="#icon-builtin"></use></svg><span>Built-in Functions vs. User-Defined Functions</span></a></li>
<li><a href="#functions-with-and-without-return"><svg class="icon"><use href="#icon-return"></use></svg><span>Functions With and Without return</span></a></li>
<li><a href="#scope-and-lifetime-of-variables"><svg class="icon"><use href="#icon-scope"></use></svg><span>Scope and Lifetime of Variables</span></a></li>
<li><a href="#real-world-examples"><svg class="icon"><use href="#icon-world"></use></svg><span>Real-World Examples</span></a></li>
<li><a href="#quick-reference-summary"><svg class="icon"><use href="#icon-table"></use></svg><span>Quick Reference Summary</span></a></li>
</ul>
</nav>
<main class="article">
<p>This guide covers every concept behind Python functions in detail, verified against how Python actually runs, with real examples from YouTube, Amazon, Uber, and Google.</p>
<hr />
<h2 id="the-anatomy-of-a-function"><span class="h-icon"><svg class="icon"><use href="#icon-anatomy"></use></svg></span>The Anatomy of a Function</h2>
<p>Every function is built from the same set of parts:</p>
<div class="diagram-card"><pre><code> def maximum ( num1, num2 ) :
│ │ │ │
│ │ │ └─ colon: ends the function header
│ │ └─ parameters: the inputs the function accepts
│ └─ function name: how you'll call it later
└─ keyword that starts every function definition
"""Returns whichever number is larger.""" ← docstring (optional)
if num1 > num2:
return num1 ← the actual logic
else:
return num2 ← return sends a value back out
</code></pre></div>
<div class="table-wrap"><table>
<thead>
<tr>
<th>Part</th>
<th>Required?</th>
<th>What it does</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>def</code></td>
<td>Yes</td>
<td>Marks the start of a function definition</td>
</tr>
<tr>
<td>Function name</td>
<td>Yes</td>
<td>How you'll refer to and call the function later</td>
</tr>
<tr>
<td>Parameters</td>
<td>No</td>
<td>The inputs the function accepts; a function can take zero</td>
</tr>
<tr>
<td><code>:</code></td>
<td>Yes</td>
<td>Ends the header line</td>
</tr>
<tr>
<td>Docstring</td>
<td>No</td>
<td>A short description of what the function does</td>
</tr>
<tr>
<td>Body (statements)</td>
<td>Yes</td>
<td>The actual logic that runs</td>
</tr>
<tr>
<td><code>return</code></td>
<td>No</td>
<td>Sends a value back to wherever the function was called from</td>
</tr>
</tbody>
</table></div>
<p>A function needs <em>either</em> <code>return</code> or <code>print</code> to actually be useful, but strictly speaking, neither is syntactically required; a function with no <code>return</code> simply hands back <code>None</code>.</p>
<h3 id="definition-vs-call-two-separate-events">Definition vs. Call: Two Separate Events</h3>
<p>Writing a function and running it are two completely different steps, and it's worth seeing that distinction directly:</p>
<div class="code-card"><div class="code-head"><div class="dots"><span></span><span></span><span></span></div><span class="lang">python</span><button class="copy-btn" type="button"><svg class="icon"><use href="#icon-copy"></use></svg>Copy</button></div><pre><code><span class="nb">print</span><span class="p">(</span><span class="s1">'A function is defined.'</span><span class="p">)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">maximum</span><span class="p">(</span><span class="n">num1</span><span class="p">,</span> <span class="n">num2</span><span class="p">):</span>
<span class="k">if</span> <span class="n">num1</span> <span class="o">></span> <span class="n">num2</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">num1</span><span class="p">,</span> <span class="s1">'is greater than'</span><span class="p">,</span> <span class="n">num2</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">num2</span><span class="p">,</span> <span class="s1">'is greater than'</span><span class="p">,</span> <span class="n">num1</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'The function has been defined but not called yet.'</span><span class="p">)</span>
<span class="n">maximum</span><span class="p">(</span><span class="mi">76</span><span class="p">,</span> <span class="mi">22</span><span class="p">)</span>
</code></pre></div>
<div class="output-card"><pre><code>A function is defined.
The function has been defined but not called yet.
76 is greater than 22
</code></pre></div>
<p>Notice the code <em>inside</em> <code>maximum</code> never runs during the <code>def</code> block itself; Python just registers that the function exists. Nothing inside it executes until the moment <code>maximum(76, 22)</code> is actually called, several lines later.</p>
<div class="diagram-card"><pre><code> def maximum(...): ← DEFINITION: Python memorizes the recipe
... but doesn't cook anything yet
maximum(76, 22) ← CALL: NOW the recipe actually runs,
using 76 and 22 as the ingredients
</code></pre></div>
<hr />
<h2 id="parameters-vs-arguments"><span class="h-icon"><svg class="icon"><use href="#icon-swap"></use></svg></span>Parameters vs. Arguments</h2>
<p>These two words get used interchangeably in casual conversation, but they mean specific, different things:</p>
<ul>
<li>A <strong>parameter</strong> is the name written inside the parentheses in the function <em>definition</em>: <code>num1</code> and <code>num2</code> in <code>def maximum(num1, num2):</code>.</li>
<li>An <strong>argument</strong> is the actual value you pass in when you <em>call</em> the function: the <code>76</code> and <code>22</code> in <code>maximum(76, 22)</code>.</li>
</ul>
<p>The parameter is the labeled placeholder; the argument is what actually fills it in.</p>
<hr />
<h2 id="types-of-arguments"><span class="h-icon"><svg class="icon"><use href="#icon-positional"></use></svg></span>Types of Arguments</h2>
<p>Python gives you three different ways to pass arguments into a function, and they can be mixed.</p>
<h3 id="positional-arguments">Positional Arguments</h3>
<p>The default behavior: arguments are matched to parameters purely by their order.</p>
<div class="code-card"><div class="code-head"><div class="dots"><span></span><span></span><span></span></div><span class="lang">python</span><button class="copy-btn" type="button"><svg class="icon"><use href="#icon-copy"></use></svg>Copy</button></div><pre><code><span class="n">maximum</span><span class="p">(</span><span class="mi">76</span><span class="p">,</span> <span class="mi">22</span><span class="p">)</span> <span class="c1"># 76 becomes num1, 22 becomes num2, because of POSITION alone</span>
</code></pre></div>
<h3 id="keyword-arguments">Keyword Arguments</h3>
<p>You can name which parameter each value belongs to, which frees you from having to match the original order:</p>
<div class="code-card"><div class="code-head"><div class="dots"><span></span><span></span><span></span></div><span class="lang">python</span><button class="copy-btn" type="button"><svg class="icon"><use href="#icon-copy"></use></svg>Copy</button></div><pre><code><span class="n">maximum</span><span class="p">(</span><span class="n">num2</span><span class="o">=</span><span class="mi">43</span><span class="p">,</span> <span class="n">num1</span><span class="o">=</span><span class="mi">99</span><span class="p">)</span> <span class="c1"># order no longer matters: explicitly labeled</span>
</code></pre></div>
<div class="diagram-card"><pre><code> Positional: maximum(76, 22)
│ │
num1 ─┘ └─ num2 (matched by POSITION)
Keyword: maximum(num2=43, num1=99)
│ │
num2=43 num1=99 (matched by NAME, any order)
</code></pre></div>
<h3 id="default-arguments">Default Arguments</h3>
<p>You can give a parameter a fallback value in the function's definition, using <code>=</code>. If the caller doesn't supply that argument, the default is used instead:</p>
<div class="code-card"><div class="code-head"><div class="dots"><span></span><span></span><span></span></div><span class="lang">python</span><button class="copy-btn" type="button"><svg class="icon"><use href="#icon-copy"></use></svg>Copy</button></div><pre><code><span class="k">def</span><span class="w"> </span><span class="nf">maximum</span><span class="p">(</span><span class="n">num1</span><span class="p">,</span> <span class="n">num2</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="k">if</span> <span class="n">num1</span> <span class="o">></span> <span class="n">num2</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">num1</span><span class="p">,</span> <span class="s1">'is greater than'</span><span class="p">,</span> <span class="n">num2</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">num2</span><span class="p">,</span> <span class="s1">'is greater than'</span><span class="p">,</span> <span class="n">num1</span><span class="p">)</span>
<span class="n">maximum</span><span class="p">(</span><span class="mi">43</span><span class="p">)</span> <span class="c1"># num2 falls back to its default, 0</span>
<span class="n">maximum</span><span class="p">(</span><span class="mi">43</span><span class="p">,</span> <span class="mi">99</span><span class="p">)</span> <span class="c1"># the default is overridden with 99</span>
</code></pre></div>
<div class="output-card"><pre><code>43 is greater than 0
99 is greater than 43
</code></pre></div>
<blockquote>
<p><strong>One rule to keep in mind:</strong> once a parameter has a default value, every parameter after it must also have one. Writing <code>def maximum(num1=0, num2):</code> looks reasonable but isn't valid Python; it raises <code>SyntaxError: parameter without a default follows parameter with a default</code>. Python needs this rule so it can always tell, unambiguously, which positional arguments fill in which parameters. That's why the default-valued parameter goes last, as shown above.</p>
</blockquote>
<h3 id="arbitrary-arguments-args">Arbitrary Arguments (<code>*args</code>)</h3>
<p>Sometimes you don't know in advance how many arguments will be passed in. Prefixing a parameter with <code>*</code> collects any number of extra positional arguments into a single tuple:</p>
<div class="code-card"><div class="code-head"><div class="dots"><span></span><span></span><span></span></div><span class="lang">python</span><button class="copy-btn" type="button"><svg class="icon"><use href="#icon-copy"></use></svg>Copy</button></div><pre><code><span class="k">def</span><span class="w"> </span><span class="nf">add</span><span class="p">(</span><span class="o">*</span><span class="n">num</span><span class="p">):</span>
<span class="n">total</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">num</span><span class="p">:</span>
<span class="n">total</span> <span class="o">+=</span> <span class="n">value</span>
<span class="k">return</span> <span class="n">total</span>
<span class="n">add</span><span class="p">(</span><span class="mi">34</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">98</span><span class="p">,</span> <span class="mi">90</span><span class="p">,</span> <span class="mi">55</span><span class="p">,</span> <span class="mi">67</span><span class="p">)</span> <span class="c1"># 376: works with 6 arguments, or 2, or 20</span>
</code></pre></div>
<p>Inside the function, <code>num</code> is just a tuple holding whatever was passed in, so the <code>for</code> loop works exactly like it would over any other tuple.</p>
<hr />
<h2 id="built-in-functions-vs-user-defined-functions"><span class="h-icon"><svg class="icon"><use href="#icon-builtin"></use></svg></span>Built-in Functions vs. User-Defined Functions</h2>
<p>Every function you've written yourself so far, <code>maximum</code>, <code>add</code>, is a <strong>user-defined function</strong>. Python also ships with a large set of <strong>built-in functions</strong> that are simply already there for you to use: <code>print()</code>, <code>len()</code>, <code>input()</code>, <code>id()</code>, and <code>type()</code> are all examples you've likely already used without writing them yourself. The full list lives in the <a href="https://docs.python.org/3/library/functions.html">official Python documentation</a>. The distinction is purely about who wrote it; both kinds work exactly the same way once called.</p>
<hr />
<h2 id="functions-with-and-without-return"><span class="h-icon"><svg class="icon"><use href="#icon-return"></use></svg></span>Functions With and Without <code>return</code></h2>
<p>A function can communicate its result in two very different ways.</p>
<div class="code-card"><div class="code-head"><div class="dots"><span></span><span></span><span></span></div><span class="lang">python</span><button class="copy-btn" type="button"><svg class="icon"><use href="#icon-copy"></use></svg>Copy</button></div><pre><code><span class="k">def</span><span class="w"> </span><span class="nf">maximum</span><span class="p">(</span><span class="n">num1</span><span class="p">,</span> <span class="n">num2</span><span class="p">):</span>
<span class="k">if</span> <span class="n">num1</span> <span class="o">></span> <span class="n">num2</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">num1</span><span class="p">,</span> <span class="s1">'is greater than'</span><span class="p">,</span> <span class="n">num2</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">num2</span><span class="p">,</span> <span class="s1">'is greater than'</span><span class="p">,</span> <span class="n">num1</span><span class="p">)</span>
<span class="k">return</span> <span class="kc">None</span>
</code></pre></div>
<p>This version <em>displays</em> the answer with <code>print</code>, but hands back <code>None</code> to the program, since there's no explicit value after <code>return</code>. Compare that with a version that actually returns something usable:</p>
<div class="code-card"><div class="code-head"><div class="dots"><span></span><span></span><span></span></div><span class="lang">python</span><button class="copy-btn" type="button"><svg class="icon"><use href="#icon-copy"></use></svg>Copy</button></div><pre><code><span class="k">def</span><span class="w"> </span><span class="nf">greater</span><span class="p">(</span><span class="n">num1</span><span class="p">,</span> <span class="n">num2</span><span class="p">):</span>
<span class="k">if</span> <span class="n">num1</span> <span class="o">></span> <span class="n">num2</span><span class="p">:</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">num1</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">num2</span>
<span class="k">return</span> <span class="n">result</span>
<span class="n">returned_value</span> <span class="o">=</span> <span class="n">greater</span><span class="p">(</span><span class="mi">99</span><span class="p">,</span> <span class="mi">21</span><span class="p">)</span> <span class="c1"># returned_value now holds 99</span>
</code></pre></div>
<p>The difference matters more than it looks like it should: a <code>print</code>-only function shows you the answer once, on screen, and that's the end of it. A function that <code>return</code>s its answer hands you back an actual value you can store in a variable, pass to another function, or use in a calculation later in the program.</p>
<blockquote>
<p><strong>A typo worth watching for:</strong> it's easy to write a line like this and forget the <code>f</code> prefix:</p>
<p><code>python
print('The maximum of two given numbers {num1} and {num2} is', greater(num1, num2))</code></p>
<p>Without <code>f</code>, curly braces are just ordinary characters to Python, not a signal to substitute a variable's value, so <code>{num1}</code> and <code>{num2}</code> print completely literally instead of showing the actual numbers. The fix is one letter:</p>
<p><code>python
print(f'The maximum of two given numbers {num1} and {num2} is', greater(num1, num2))</code></p>
<p>This one is worth watching for specifically because it fails silently: no error, just wrong output.</p>
</blockquote>
<hr />
<h2 id="scope-and-lifetime-of-variables"><span class="h-icon"><svg class="icon"><use href="#icon-scope"></use></svg></span>Scope and Lifetime of Variables</h2>
<p><strong>Scope</strong> is <em>where</em> in your program a variable can be seen and used. <strong>Lifetime</strong> is <em>how long</em> it continues to exist in memory. Variables created inside a function have both limited: they're local to that function, and they're destroyed the moment the function finishes running.</p>
<div class="code-card"><div class="code-head"><div class="dots"><span></span><span></span><span></span></div><span class="lang">python</span><button class="copy-btn" type="button"><svg class="icon"><use href="#icon-copy"></use></svg>Copy</button></div><pre><code><span class="n">val</span> <span class="o">=</span> <span class="mi">50</span>
<span class="k">def</span><span class="w"> </span><span class="nf">greater</span><span class="p">(</span><span class="n">num1</span><span class="p">,</span> <span class="n">num2</span><span class="p">):</span>
<span class="k">if</span> <span class="n">num1</span> <span class="o">></span> <span class="n">num2</span><span class="p">:</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">num1</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">num2</span>
<span class="k">return</span> <span class="n">result</span>
<span class="n">greater</span><span class="p">(</span><span class="mf">999.45</span><span class="p">,</span> <span class="mf">334.32</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> <span class="c1"># NameError: name 'result' is not defined</span>
<span class="nb">print</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="c1"># 50: val was never inside the function, so it's unaffected</span>
</code></pre></div>
<div class="diagram-card"><pre><code> Outside the function Inside greater()
┌─────────────────┐ ┌─────────────────┐
│ val = 50 │ │ result = ... │
│ (stays alive │ │ (created fresh │
│ the whole time) │ │ each call, │
│ │ │ destroyed the │
│ │ │ instant the │
│ │ │ function ends) │
└─────────────────┘ └─────────────────┘
print(result) ← ERROR: result never existed out here
print(val) ← fine: val was always out here
</code></pre></div>
<p><code>result</code> was created fresh inside <code>greater</code>, handed back via <code>return</code>, and then discarded the instant the function finished. Trying to reach it from outside afterward is like trying to walk into a room that was demolished the moment you left it.</p>
<hr />
<h2 id="real-world-examples"><span class="h-icon"><svg class="icon"><use href="#icon-world"></use></svg></span>Real-World Examples</h2>
<p><strong>YouTube: Engagement Score (Positional Arguments).</strong> Ranking millions of videos means running the same formula over and over, which is exactly what a function is for. The three inputs (views, likes, comments) are always supplied in the same fixed order.</p>
<div class="code-card"><div class="code-head"><div class="dots"><span></span><span></span><span></span></div><span class="lang">python</span><button class="copy-btn" type="button"><svg class="icon"><use href="#icon-copy"></use></svg>Copy</button></div><pre><code><span class="k">def</span><span class="w"> </span><span class="nf">calculate_engagement</span><span class="p">(</span><span class="n">views</span><span class="p">,</span> <span class="n">likes</span><span class="p">,</span> <span class="n">comments</span><span class="p">):</span>
<span class="n">score</span> <span class="o">=</span> <span class="n">views</span> <span class="o">+</span> <span class="p">(</span><span class="n">likes</span> <span class="o">*</span> <span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">comments</span> <span class="o">*</span> <span class="mi">5</span><span class="p">)</span>
<span class="k">return</span> <span class="n">score</span>
<span class="n">video_a_score</span> <span class="o">=</span> <span class="n">calculate_engagement</span><span class="p">(</span><span class="mi">10000</span><span class="p">,</span> <span class="mi">500</span><span class="p">,</span> <span class="mi">50</span><span class="p">)</span>
<span class="n">video_b_score</span> <span class="o">=</span> <span class="n">calculate_engagement</span><span class="p">(</span><span class="mi">5000</span><span class="p">,</span> <span class="mi">800</span><span class="p">,</span> <span class="mi">120</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Video A Engagement Score: </span><span class="si">{</span><span class="n">video_a_score</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Video B Engagement Score: </span><span class="si">{</span><span class="n">video_b_score</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
</code></pre></div>
<div class="output-card"><pre><code>Video A Engagement Score: 11250
Video B Engagement Score: 7200
</code></pre></div>
<p><strong>Amazon: Checkout Calculator (Keyword Arguments).</strong> With four separate dollar amounts going into one function, keyword arguments remove any risk of accidentally swapping two of them.</p>
<div class="code-card"><div class="code-head"><div class="dots"><span></span><span></span><span></span></div><span class="lang">python</span><button class="copy-btn" type="button"><svg class="icon"><use href="#icon-copy"></use></svg>Copy</button></div><pre><code><span class="k">def</span><span class="w"> </span><span class="nf">calculate_checkout_total</span><span class="p">(</span><span class="n">subtotal</span><span class="p">,</span> <span class="n">tax_rate</span><span class="p">,</span> <span class="n">discount</span><span class="p">,</span> <span class="n">shipping</span><span class="p">):</span>
<span class="n">tax_amount</span> <span class="o">=</span> <span class="n">subtotal</span> <span class="o">*</span> <span class="n">tax_rate</span>
<span class="n">total</span> <span class="o">=</span> <span class="n">subtotal</span> <span class="o">+</span> <span class="n">tax_amount</span> <span class="o">-</span> <span class="n">discount</span> <span class="o">+</span> <span class="n">shipping</span>
<span class="k">return</span> <span class="n">total</span>
<span class="n">final_price</span> <span class="o">=</span> <span class="n">calculate_checkout_total</span><span class="p">(</span>
<span class="n">shipping</span><span class="o">=</span><span class="mf">5.00</span><span class="p">,</span>
<span class="n">discount</span><span class="o">=</span><span class="mf">10.00</span><span class="p">,</span>
<span class="n">subtotal</span><span class="o">=</span><span class="mf">100.00</span><span class="p">,</span>
<span class="n">tax_rate</span><span class="o">=</span><span class="mf">0.08</span>
<span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Your final Amazon total is: $</span><span class="si">{</span><span class="n">final_price</span><span class="si">:</span><span class="s2">.2f</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
</code></pre></div>
<div class="output-card"><pre><code>Your final Amazon total is: $103.00
</code></pre></div>
<p><strong>Uber: Fare Estimator (Positional + Default Arguments).</strong> Most riders just want the standard option, so <code>ride_type</code> defaults to <code>"UberX"</code> and only needs to be supplied when a rider picks something else.</p>
<div class="code-card"><div class="code-head"><div class="dots"><span></span><span></span><span></span></div><span class="lang">python</span><button class="copy-btn" type="button"><svg class="icon"><use href="#icon-copy"></use></svg>Copy</button></div><pre><code><span class="k">def</span><span class="w"> </span><span class="nf">estimate_fare</span><span class="p">(</span><span class="n">distance_km</span><span class="p">,</span> <span class="n">ride_type</span><span class="o">=</span><span class="s2">"UberX"</span><span class="p">):</span>
<span class="n">base_fare</span> <span class="o">=</span> <span class="mf">5.00</span>
<span class="n">per_km_rate</span> <span class="o">=</span> <span class="mf">2.00</span>
<span class="n">estimate</span> <span class="o">=</span> <span class="n">base_fare</span> <span class="o">+</span> <span class="p">(</span><span class="n">distance_km</span> <span class="o">*</span> <span class="n">per_km_rate</span><span class="p">)</span>
<span class="k">if</span> <span class="n">ride_type</span> <span class="o">==</span> <span class="s2">"UberXL"</span><span class="p">:</span>
<span class="n">estimate</span> <span class="o">*=</span> <span class="mf">1.5</span>
<span class="k">elif</span> <span class="n">ride_type</span> <span class="o">==</span> <span class="s2">"Black"</span><span class="p">:</span>
<span class="n">estimate</span> <span class="o">*=</span> <span class="mf">2.5</span>
<span class="k">return</span> <span class="n">estimate</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Standard UberX: $</span><span class="si">{</span><span class="n">estimate_fare</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span><span class="si">:</span><span class="s2">.2f</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Large group (UberXL): $</span><span class="si">{</span><span class="n">estimate_fare</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="s1">'UberXL'</span><span class="p">)</span><span class="si">:</span><span class="s2">.2f</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Luxury (Uber Black): $</span><span class="si">{</span><span class="n">estimate_fare</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="s1">'Black'</span><span class="p">)</span><span class="si">:</span><span class="s2">.2f</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
</code></pre></div>
<div class="output-card"><pre><code>Standard UberX: $25.00
Large group (UberXL): $37.50
Luxury (Uber Black): $62.50
</code></pre></div>
<p><strong>Google: Search Query Cleanup (Helper Functions).</strong> Breaking one task into two small functions, one that cleans text and one that runs the search, is a "helper function" pattern: each function does exactly one narrow thing, and the second function calls the first.</p>
<div class="code-card"><div class="code-head"><div class="dots"><span></span><span></span><span></span></div><span class="lang">python</span><button class="copy-btn" type="button"><svg class="icon"><use href="#icon-copy"></use></svg>Copy</button></div><pre><code><span class="k">def</span><span class="w"> </span><span class="nf">clean_search_query</span><span class="p">(</span><span class="n">raw_query</span><span class="p">):</span>
<span class="n">cleaned</span> <span class="o">=</span> <span class="n">raw_query</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
<span class="k">return</span> <span class="n">cleaned</span>
<span class="k">def</span><span class="w"> </span><span class="nf">execute_google_search</span><span class="p">(</span><span class="n">user_input</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Raw input received: '</span><span class="si">{</span><span class="n">user_input</span><span class="si">}</span><span class="s2">'"</span><span class="p">)</span>
<span class="n">search_term</span> <span class="o">=</span> <span class="n">clean_search_query</span><span class="p">(</span><span class="n">user_input</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Searching database for: '</span><span class="si">{</span><span class="n">search_term</span><span class="si">}</span><span class="s2">'..."</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Returning 10,000 results.</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="n">execute_google_search</span><span class="p">(</span><span class="s2">" MaCHinE LeaRning bAsiCs "</span><span class="p">)</span>
<span class="n">execute_google_search</span><span class="p">(</span><span class="s2">" HOW to WRite a FUnctioN "</span><span class="p">)</span>
</code></pre></div>
<div class="output-card"><pre><code>Raw input received: ' MaCHinE LeaRning bAsiCs '
Searching database for: 'machine learning basics'...
Returning 10,000 results.
Raw input received: ' HOW to WRite a FUnctioN '
Searching database for: 'how to write a function'...
Returning 10,000 results.
</code></pre></div>
<hr />
<h2 id="quick-reference-summary"><span class="h-icon"><svg class="icon"><use href="#icon-table"></use></svg></span>Quick Reference Summary</h2>
<div class="table-wrap"><table>
<thead>
<tr>
<th>Concept</th>
<th>Syntax</th>
<th>What it does</th>
</tr>
</thead>
<tbody>
<tr>
<td>Definition</td>
<td><code>def name(params):</code></td>
<td>Registers a reusable block of code</td>
</tr>
<tr>
<td>Call</td>
<td><code>name(arguments)</code></td>
<td>Actually runs the code inside the function</td>
</tr>
<tr>
<td>Parameter</td>
<td>Written in the <code>def</code> line</td>
<td>The labeled placeholder for an input</td>
</tr>
<tr>
<td>Argument</td>
<td>Written in the call</td>
<td>The actual value supplied for a parameter</td>
</tr>
<tr>
<td>Positional argument</td>
<td><code>func(a, b)</code></td>
<td>Matched to parameters by order</td>
</tr>
<tr>
<td>Keyword argument</td>
<td><code>func(b=2, a=1)</code></td>
<td>Matched to parameters by name, any order</td>
</tr>
<tr>
<td>Default argument</td>
<td><code>def func(a, b=0):</code></td>
<td>Falls back to a value if the caller omits it (must come after all non-default parameters)</td>
</tr>
<tr>
<td>Arbitrary arguments</td>
<td><code>def func(*args):</code></td>
<td>Collects any number of extra positional arguments into a tuple</td>
</tr>
<tr>
<td><code>return</code></td>
<td><code>return value</code></td>
<td>Sends a value back to the caller, usable elsewhere in the program</td>
</tr>
<tr>
<td>No <code>return</code></td>
<td>(nothing, or <code>return</code> alone)</td>
<td>The function returns <code>None</code></td>
</tr>
<tr>
<td>Local scope</td>
<td>Any variable created inside a function</td>
<td>Only exists, and is only visible, inside that function</td>
</tr>
<tr>
<td>Built-in function</td>
<td><code>print()</code>, <code>len()</code>, <code>type()</code>...</td>
<td>Ships with Python; no need to define it yourself</td>
</tr>
</tbody>
</table></div>
<hr />
<p>Definition and call are two separate moments; parameters and arguments are two separate things; and a variable created inside a function has a life that begins and ends with that single call. Once those three distinctions are solid, the different argument styles (positional, keyword, default, arbitrary) stop looking like separate rules to memorize and start looking like different answers to the same question: how should this particular input reach the function?</p>
<div class="article-end">
<p><strong>Definition and call are two separate moments; parameters and arguments are two separate things.</strong> Once those distinctions are solid, positional, keyword, default, and arbitrary arguments stop looking like separate rules to memorize and start looking like different answers to the same question: how should this input reach the function?</p>
</div>
<nav class="series-nav">
<a class="prev" href="5_conditionals_loops.html"><span class="dir">← Previous</span><span class="name">Conditional Statements and Loops</span></a>
<a class="next" href="7_types_of_functions.html"><span class="dir">Next →</span><span class="name">Types of Functions in Python</span></a>
<a class="all" href="index.html">All ten articles</a>
</nav>
</main>
</div>
</div>
<footer class="site-footer">
<div class="f-left">Python Programming Series / Article 6 of the course</div>
<div class="f-right"><a href="#top">Back to top ↑</a></div>
</footer>
<script>
(function(){
var t=document.getElementById('navToggle'),s=document.getElementById('sidebar');
if(t&&s){t.addEventListener('click',function(){s.classList.toggle('open');var o=s.classList.contains('open');t.innerHTML=o?'<svg class="icon"><use href="#icon-close"></use></svg>':'<svg class="icon"><use href="#icon-menu"></use></svg>';});s.querySelectorAll('a').forEach(function(a){a.addEventListener('click',function(){s.classList.remove('open');t.innerHTML='<svg class="icon"><use href="#icon-menu"></use></svg>';});});}
var tl=Array.prototype.slice.call(document.querySelectorAll('.toc a')),sc=tl.map(function(a){return document.getElementById(a.getAttribute('href').slice(1));}).filter(Boolean);
if('IntersectionObserver' in window&&sc.length){var bi={};tl.forEach(function(a){bi[a.getAttribute('href').slice(1)]=a;});var obs=new IntersectionObserver(function(es){es.forEach(function(e){var l=bi[e.target.id];if(!l)return;if(e.isIntersecting){tl.forEach(function(a){a.classList.remove('active');});l.classList.add('active');}});},{rootMargin:'-15% 0px -70% 0px',threshold:0});sc.forEach(function(s){obs.observe(s);});}
document.querySelectorAll('.code-card').forEach(function(c){var b=c.querySelector('.copy-btn'),e=c.querySelector('code');if(!b||!e)return;b.addEventListener('click',function(){var tx=e.innerText,done=function(){var o=b.innerHTML;b.innerHTML='<svg class="icon"><use href="#icon-check"></use></svg> Copied';setTimeout(function(){b.innerHTML=o;},1600);};if(navigator.clipboard&&navigator.clipboard.writeText)navigator.clipboard.writeText(tx).then(done,done);else{var ta=document.createElement('textarea');ta.value=tx;document.body.appendChild(ta);ta.select();try{document.execCommand('copy');}catch(x){}document.body.removeChild(ta);done();}});});
})();
</script>
</body>
</html>