summaryrefslogtreecommitdiff
path: root/Practical_C_Programming/Chapter_9_4.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Practical_C_Programming/Chapter_9_4.ipynb')
-rw-r--r--Practical_C_Programming/Chapter_9_4.ipynb95
1 files changed, 81 insertions, 14 deletions
diff --git a/Practical_C_Programming/Chapter_9_4.ipynb b/Practical_C_Programming/Chapter_9_4.ipynb
index 28d47037..8d18a77b 100644
--- a/Practical_C_Programming/Chapter_9_4.ipynb
+++ b/Practical_C_Programming/Chapter_9_4.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 9"
+ "name": "",
+ "signature": "sha256:312f9e5b3211fd50ff3dd7c27b81824ed2a8d6172aff8fb27752ffb6ee205caa"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,47 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 9: Variable scope and functions"
+ "source": [
+ "Chapter 9: Variable scope and functions"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 9.1, Page number: 160"
+ "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",
+ "input": [
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def func() :\n",
+ " if not hasattr(func, \"permanent\") :\n",
+ " func.permanent = 1\n",
+ " result = func.permanent\n",
+ " func.permanent += 1\n",
+ " return result\n",
+ "\n",
+ "for 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"
+ "text": [
+ "Temporary 1 Permanent 1\n",
+ "Temporary 1 Permanent 2\n",
+ "Temporary 1 Permanent 3\n"
+ ]
}
],
"prompt_number": 1
@@ -38,19 +61,36 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 9.3, Page number: 164"
+ "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)))",
+ "input": [
+ "\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def triangle (width, height) :\n",
+ " area = width * height / 2.0\n",
+ " return area\n",
+ "\n",
+ "print ('Triangle #1 %f' % (triangle (1.3, 8.3)))\n",
+ "print ('Triangle #2 %f' % (triangle (4.8, 9.8)))\n",
+ "print ('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"
+ "text": [
+ "Triangle #1 5.395000\n",
+ "Triangle #2 23.520000\n",
+ "Triangle #3 1.200000\n"
+ ]
}
],
"prompt_number": 2
@@ -59,19 +99,32 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 9.4, Page number: 166"
+ "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))",
+ "input": [
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def length (string) :\n",
+ " return len(string)\n",
+ "\n",
+ "line = 'hello world' \n",
+ "\n",
+ "print ('Length is: %d' % length(line))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Length is: 11\n"
+ "text": [
+ "Length is: 11\n"
+ ]
}
],
"prompt_number": 4
@@ -80,19 +133,33 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 9.6, Page number: 170"
+ "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))",
+ "input": [
+ "\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def length (string) :\n",
+ " return len(string)\n",
+ "\n",
+ "line = 'Steve Oualline'\n",
+ "\n",
+ "print ('Length is: %d' % length(line))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Length is: 14\n"
+ "text": [
+ "Length is: 14\n"
+ ]
}
],
"prompt_number": 5