summaryrefslogtreecommitdiff
path: root/The_C_Book/Chapter4.ipynb
blob: 878ed94c160e637b1347891016a50a998bb02cbd (plain)
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
{
 "metadata": {
  "name": "",
  "signature": "sha256:619769e887714e6791182739416189f2f7fa6743af97af5b6710a3d8f08e8d46"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 4: Functions"
     ]
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Example 4.1, page no. 103"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def aax1():\n",
      "# code for function body \n",
      "    return (1.0)\n",
      "\n",
      "r = aax1();\n",
      "print r"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1.0\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.2 & 4.3, page no. 106-108"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "This won't generate any output\n",
      "Examples 4.2 and 4.3 are same for python language.\n",
      "'''\n",
      "\n",
      "\n",
      "def pmax(a1,a2):\n",
      "    biggest = 0\n",
      "    if(a1 > a2):\n",
      "        biggest = a1\n",
      "    else:\n",
      "        biggest = a2\n",
      "\n",
      "#print \"larger of %d and %d is %d\\n\" %(a1, a2, biggest);\n",
      "\n",
      "\n",
      "\n",
      "for i in range(11):\n",
      "    for j in range(-10,11):\n",
      "       pmax(i,j)\n",
      "        \n",
      "# It shows that all variables scope is locally only. They can not be accessed from outside its scope.        "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.4, page no. 109"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def sq_root(x):\n",
      "    DELTA = 0.0001\n",
      "    curr_appx = 0\n",
      "    last_appx = 0\n",
      "    diff = 0\n",
      "    last_appx = x\n",
      "    diff = DELTA+1\n",
      "    while(diff > DELTA):\n",
      "        curr_appx = 0.5*(last_appx + x/last_appx)\n",
      "        diff = curr_appx - last_appx\n",
      "        if(diff < 0):\n",
      "            diff = -diff\n",
      "            last_appx = curr_appx\n",
      "    return(curr_appx)\n",
      " \n",
      "\n",
      "\n",
      "for i in range(1,101):\n",
      "    print \"root of %d is %f\\n\" %( i, sq_root(i))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "root of 1 is 1.000000\n",
        "\n",
        "root of 2 is 1.414214\n",
        "\n",
        "root of 3 is 1.732051\n",
        "\n",
        "root of 4 is 2.000000\n",
        "\n",
        "root of 5 is 2.236068\n",
        "\n",
        "root of 6 is 2.449490\n",
        "\n",
        "root of 7 is 2.645751\n",
        "\n",
        "root of 8 is 2.828427\n",
        "\n",
        "root of 9 is 3.000000\n",
        "\n",
        "root of 10 is 3.162278\n",
        "\n",
        "root of 11 is 3.316625\n",
        "\n",
        "root of 12 is 3.464102\n",
        "\n",
        "root of 13 is 3.605551\n",
        "\n",
        "root of 14 is 3.741657\n",
        "\n",
        "root of 15 is 3.872983\n",
        "\n",
        "root of 16 is 4.000000\n",
        "\n",
        "root of 17 is 4.123106\n",
        "\n",
        "root of 18 is 4.242641\n",
        "\n",
        "root of 19 is 4.358899\n",
        "\n",
        "root of 20 is 4.472136\n",
        "\n",
        "root of 21 is 4.582576\n",
        "\n",
        "root of 22 is 4.690416\n",
        "\n",
        "root of 23 is 4.795832\n",
        "\n",
        "root of 24 is 4.898979\n",
        "\n",
        "root of 25 is 5.000000\n",
        "\n",
        "root of 26 is 5.099020\n",
        "\n",
        "root of 27 is 5.196152\n",
        "\n",
        "root of 28 is 5.291503\n",
        "\n",
        "root of 29 is 5.385165\n",
        "\n",
        "root of 30 is 5.477226\n",
        "\n",
        "root of 31 is 5.567764\n",
        "\n",
        "root of 32 is 5.656854\n",
        "\n",
        "root of 33 is 5.744563\n",
        "\n",
        "root of 34 is 5.830952\n",
        "\n",
        "root of 35 is 5.916080\n",
        "\n",
        "root of 36 is 6.000000\n",
        "\n",
        "root of 37 is 6.082763\n",
        "\n",
        "root of 38 is 6.164414\n",
        "\n",
        "root of 39 is 6.244998\n",
        "\n",
        "root of 40 is 6.324555\n",
        "\n",
        "root of 41 is 6.403124\n",
        "\n",
        "root of 42 is 6.480741\n",
        "\n",
        "root of 43 is 6.557439\n",
        "\n",
        "root of 44 is 6.633250\n",
        "\n",
        "root of 45 is 6.708204\n",
        "\n",
        "root of 46 is 6.782330\n",
        "\n",
        "root of 47 is 6.855655\n",
        "\n",
        "root of 48 is 6.928203\n",
        "\n",
        "root of 49 is 7.000000\n",
        "\n",
        "root of 50 is 7.071068\n",
        "\n",
        "root of 51 is 7.141428\n",
        "\n",
        "root of 52 is 7.211103\n",
        "\n",
        "root of 53 is 7.280110\n",
        "\n",
        "root of 54 is 7.348469\n",
        "\n",
        "root of 55 is 7.416198\n",
        "\n",
        "root of 56 is 7.483315\n",
        "\n",
        "root of 57 is 7.549834\n",
        "\n",
        "root of 58 is 7.615773\n",
        "\n",
        "root of 59 is 7.681146\n",
        "\n",
        "root of 60 is 7.745967\n",
        "\n",
        "root of 61 is 7.810250\n",
        "\n",
        "root of 62 is 7.874008\n",
        "\n",
        "root of 63 is 7.937254\n",
        "\n",
        "root of 64 is 8.000000\n",
        "\n",
        "root of 65 is 8.062258\n",
        "\n",
        "root of 66 is 8.124038\n",
        "\n",
        "root of 67 is 8.185353\n",
        "\n",
        "root of 68 is 8.246211\n",
        "\n",
        "root of 69 is 8.306624\n",
        "\n",
        "root of 70 is 8.366600\n",
        "\n",
        "root of 71 is 8.426150\n",
        "\n",
        "root of 72 is 8.485281\n",
        "\n",
        "root of 73 is 8.544004\n",
        "\n",
        "root of 74 is 8.602325\n",
        "\n",
        "root of 75 is 8.660254\n",
        "\n",
        "root of 76 is 8.717798\n",
        "\n",
        "root of 77 is 8.774964\n",
        "\n",
        "root of 78 is 8.831761\n",
        "\n",
        "root of 79 is 8.888194\n",
        "\n",
        "root of 80 is 8.944272\n",
        "\n",
        "root of 81 is 9.000000\n",
        "\n",
        "root of 82 is 9.055385\n",
        "\n",
        "root of 83 is 9.110434\n",
        "\n",
        "root of 84 is 9.165151\n",
        "\n",
        "root of 85 is 9.219544\n",
        "\n",
        "root of 86 is 9.273618\n",
        "\n",
        "root of 87 is 9.327379\n",
        "\n",
        "root of 88 is 9.380832\n",
        "\n",
        "root of 89 is 9.433981\n",
        "\n",
        "root of 90 is 9.486833\n",
        "\n",
        "root of 91 is 9.539392\n",
        "\n",
        "root of 92 is 9.591663\n",
        "\n",
        "root of 93 is 9.643651\n",
        "\n",
        "root of 94 is 9.695360\n",
        "\n",
        "root of 95 is 9.746794\n",
        "\n",
        "root of 96 is 9.797959\n",
        "\n",
        "root of 97 is 9.848858\n",
        "\n",
        "root of 98 is 9.899495\n",
        "\n",
        "root of 99 is 9.949874\n",
        "\n",
        "root of 100 is 10.000000\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.6, page no. 115"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "def called_func(a,b):\n",
      "    temp = a * b\n",
      "    print temp\n",
      "\n",
      "called_func(1,2*3.5)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7.0\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.7, page no. 116"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def changer(x):\n",
      "    while(x!=0):\n",
      "        print \"changer: x=%d\\n\" %(x)\n",
      "        x-=1\n",
      "\n",
      "i = 5\n",
      "print \"before i=%d\\n\" %( i);\n",
      "changer(i);\n",
      "print \"after i=%d\\n\" %( i);"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "before i=5\n",
        "\n",
        "changer: x=5\n",
        "\n",
        "changer: x=4\n",
        "\n",
        "changer: x=3\n",
        "\n",
        "changer: x=2\n",
        "\n",
        "changer: x=1\n",
        "\n",
        "after i=5\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.8, page no. 120"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "Do not try to run....\n",
      "It's infinite looping program..\n",
      "\n",
      "This program shows how a function calls itself.\n",
      "'''\n",
      "\n",
      "def expr():\n",
      "    val = 0\n",
      "    ch_in = 0\n",
      "    val = mul_exp()\n",
      "    while True:\n",
      "        ch_in = raw_input(\"Enter + or - \")\n",
      "        if ch_in =='+':\n",
      "            val = val + mul_exp();\n",
      "        else:\n",
      "            if ch_in == '-':\n",
      "                val = val - mul_exp()\n",
      "            else:\n",
      "                return val\n",
      "\n",
      "    return val\n",
      "\n",
      "def mul_exp():\n",
      "    val = 0\n",
      "    ch_in = 0\n",
      "    val = unary_exp()\n",
      "    while True:\n",
      "        ch_in = raw_input(\"Enter * or / or % \")\n",
      "        if ch_in =='*':\n",
      "            val = val * unary_exp();\n",
      "        else:\n",
      "            if ch_in == '/':\n",
      "                val = val - mul_exp()\n",
      "            else:\n",
      "                if ch_in =='%':\n",
      "                    val = val % unary_exp()\n",
      "                else:\n",
      "                    return val\n",
      "    return val\n",
      "\n",
      "def unary_exp():\n",
      "    val=0\n",
      "    ch_in = raw_input(\"Enter + or - \")\n",
      "    if ch_in =='+':\n",
      "        val = unary_exp()\n",
      "    else:\n",
      "        if ch_in == '-':\n",
      "            val = -unary_exp()\n",
      "        else:\n",
      "            val = primary()\n",
      "            return val\n",
      "\n",
      "    return val\n",
      "\n",
      "def primary():\n",
      "    val = 0\n",
      "    ch_in = raw_input('Enter value')\n",
      "    if (ch_in >= '0' and ch_in <= '9'):\n",
      "        val = ch_in - '0'\n",
      "        return val\n",
      "    if ch_in == '(':\n",
      "        val = expr()\n",
      "    print \"error: primary read %d\\n\" %ch_in\n",
      "    return val\n",
      "\n",
      "\n",
      "val = 0\n",
      "while True:\n",
      "    print \"expression: \"\n",
      "    val = expr();\n",
      "    if(raw_input()!= '\\n'):\n",
      "        print \"error\\n\"\n",
      "        while(raw_input() != '\\n'):\n",
      "            pass\n",
      "        \n",
      "    else:\n",
      "        print \"result is %d\\n\" %val"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.9, page no. 123"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#No output will be produced\n",
      "\n",
      "from second import * #importing all content of second.py file\n",
      "\n",
      "\n",
      "i=0\n",
      "print f_in_other_place(i) # declaration "
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.10, page no. 124"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "Example 4.10\n",
      "/* example library module */\n",
      "/* only 'callable' is visible outside */\n",
      "'''\n",
      "\n",
      "\n",
      "__buf=[]\n",
      "__length = 0\n",
      "\n",
      "def __fillup__():\n",
      "    while (length <100):\n",
      "        buf[length] = 0\n",
      "                length += 1\n",
      "\n",
      "def callable ():\n",
      "    if (length ==0):\n",
      "        fillup ()\n",
      "                a = buf[length]\n",
      "                length-=1\n",
      "        return (a)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.11, page no. 126"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "python doesn't have static variable \n",
      "we can use global variable for counting \n",
      "this demonstrates how it works...\n",
      "'''\n",
      "\n",
      "count = 0\n",
      "\n",
      "def small_val ():\n",
      "    count+=1\n",
      "    return (count % 16)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.12, page no. 126"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "Example 4.12\n",
      "python doesn't have static variables.\n",
      "'''\n",
      "\n",
      "\n",
      "depth=0\n",
      "def  r_func():\n",
      "    depth+=1\n",
      "    if (depth > 200):\n",
      "        print (\"excessive recursion\\n\")\n",
      "        sys.exit(0)\t\n",
      "    else:\n",
      "        '''/* do usual thing,\n",
      "        * not shown here.\n",
      "        * This last action\n",
      "        * occasionally results in another\n",
      "        * call on r_func()\n",
      "        */\n",
      "        '''\n",
      "        x_func()\n",
      "\n",
      "    depth-=1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}