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.ipynb104
1 files changed, 104 insertions, 0 deletions
diff --git a/Practical_C_Programming/Chapter_9_4.ipynb b/Practical_C_Programming/Chapter_9_4.ipynb
new file mode 100644
index 00000000..28d47037
--- /dev/null
+++ b/Practical_C_Programming/Chapter_9_4.ipynb
@@ -0,0 +1,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": {}
+ }
+ ]
+} \ No newline at end of file