summaryrefslogtreecommitdiff
path: root/Practical_C_Programming/Chapter_9_4.ipynb
blob: 28d47037b4f8074f1d59b4c601009131791c1033 (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
{
 "metadata": {
  "name": "Chapter 9"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": "Chapter 9: Variable scope and functions"
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "Example 9.1, Page number: 160"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "# Example 9.1.py\n# To illustrate the difference between temporary and permanent variables\n\n\n# Function declaration, calculation and result\ndef func() :\n   if not hasattr(func, \"permanent\") :\n      func.permanent = 1\n   result = func.permanent\n   func.permanent += 1\n   return result\n\nfor counter in range (0, 3) :\n   temporary = 1\n   print ('Temporary %d Permanent %d' % (temporary, func()))\n   temporary += 1",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Temporary 1 Permanent 1\nTemporary 1 Permanent 2\nTemporary 1 Permanent 3\n"
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "Example 9.3, Page number: 164"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "# Example 9.3.py\n# To compute the area of a triangle\n\n\n# Function declaration, calculation and result\ndef triangle (width, height) :\n   area = width * height / 2.0\n   return area\n\nprint ('Triangle #1 %f' % (triangle (1.3, 8.3)))\nprint ('Triangle #2 %f' % (triangle (4.8, 9.8)))\nprint ('Triangle #3 %f' % (triangle (1.2, 2.0)))",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Triangle #1 5.395000\nTriangle #2 23.520000\nTriangle #3 1.200000\n"
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "Example 9.4, Page number: 166"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "# Example 9.4.py\n# To compute the length of a string\n\n\n# Function declaration, calculation and result\ndef length (string) :\n   return len(string)\n\nline = 'hello world'   \n\nprint ('Length is: %d' % length(line))",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Length is: 11\n"
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "Example 9.6, Page number: 170"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "# Example 9.6.py\n# To compute the length of a string\n\n\n# Function declaration, calculation and result\ndef length (string) :\n   return len(string)\n\nline = 'Steve Oualline'\n\nprint ('Length is: %d' % length(line))",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Length is: 14\n"
      }
     ],
     "prompt_number": 5
    }
   ],
   "metadata": {}
  }
 ]
}