summaryrefslogtreecommitdiff
path: root/C++_Demystified:_A_Self-Teaching_Guide/chapter9.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'C++_Demystified:_A_Self-Teaching_Guide/chapter9.ipynb')
-rw-r--r--C++_Demystified:_A_Self-Teaching_Guide/chapter9.ipynb1036
1 files changed, 1036 insertions, 0 deletions
diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter9.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter9.ipynb
new file mode 100644
index 00000000..4918a533
--- /dev/null
+++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter9.ipynb
@@ -0,0 +1,1036 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 9 - Functions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.1, page no. 179"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "terminology of a function\n",
+ "note: there is no need of main in python, defining here just for sake of understanding\n",
+ "\"\"\"\n",
+ "\n",
+ "def main(): #defining a function \n",
+ " print \"Hello World!\"\n",
+ "\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello World!\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.2, page no. 180"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "defining a function\n",
+ "\"\"\"\n",
+ "\n",
+ "def printMessage(): # defining function\n",
+ " print \"Hello world!\"\n",
+ "\n",
+ "printMessage() # calling the function"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello world!\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.3, page no. 181"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "prototyping\n",
+ "note: there is no need of prototyping in Python. In fact, you have to define a function before you call it in Python.\n",
+ "\"\"\"\n",
+ "\n",
+ "printMessage();\n",
+ "\n",
+ "def printMessage():\n",
+ " print \"Hello world!\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello world!\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.4, page no. 182"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "prototyping\n",
+ "note: the way it is given in textbook is not possible in python. We have to \n",
+ "define a funciton first in order to call it\n",
+ "\"\"\"\n",
+ "\n",
+ "def printMessage():\n",
+ " print \"Hello world!\"\n",
+ "\n",
+ "printMessage()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello world!\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.5, page no. 184"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "local variables\n",
+ "note: program will give an error saying local variable time is not defined\n",
+ "\"\"\"\n",
+ "\n",
+ "def printMessage():\n",
+ " times += 1\n",
+ " print \"This function called \", times, \" times\"\n",
+ "\n",
+ "\n",
+ "times = 0\n",
+ "choice = '1'\n",
+ "while(choice != 'Q'):\n",
+ " print \"Enter Q to quit, any other character to continue: \",\n",
+ " choice = raw_input()\n",
+ " if (choice == 'Q'):\n",
+ " print \"Input stopped\"\n",
+ " else:\n",
+ " printMessage()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Q to quit, any other character to continue: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "e\n"
+ ]
+ },
+ {
+ "ename": "UnboundLocalError",
+ "evalue": "local variable 'times' referenced before assignment",
+ "output_type": "pyerr",
+ "traceback": [
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)",
+ "\u001b[1;32m<ipython-input-8-42e131eb489c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"Input stopped\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 19\u001b[1;33m \u001b[0mprintMessage\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
+ "\u001b[1;32m<ipython-input-8-42e131eb489c>\u001b[0m in \u001b[0;36mprintMessage\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mprintMessage\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mtimes\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"This function called \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtimes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\" times\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
+ "\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'times' referenced before assignment"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.6, page no. 185"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "local variables\n",
+ "\"\"\"\n",
+ "\n",
+ "def printMessage():\n",
+ " times = 0\n",
+ " times += 1\n",
+ " print \"This function called \", times, \" times\"\n",
+ "\n",
+ "\n",
+ "choice = '1'\n",
+ "while(choice != 'Q'):\n",
+ " print \"Enter Q to quit, any other character to continue: \",\n",
+ " choice = raw_input()\n",
+ " if (choice == 'Q'):\n",
+ " print \"Input stopped\"\n",
+ " else:\n",
+ " printMessage()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter Q to quit, any other character to continue: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "w\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " This function called 1 times\n",
+ "Enter Q to quit, any other character to continue: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "q\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " This function called 1 times\n",
+ "Enter Q to quit, any other character to continue: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Q\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Input stopped\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.7, page no. 186"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "global variables\n",
+ "\"\"\"\n",
+ "\n",
+ "times = 0\n",
+ "def printMessage():\n",
+ " global times\n",
+ " times += 1\n",
+ " print \"This function called \", times, \" times\"\n",
+ "\n",
+ "\n",
+ "choice = '1'\n",
+ "while(choice != 'Q'):\n",
+ " print \"Enter Q to quit, any other character to continue: \",\n",
+ " choice = raw_input()\n",
+ " if (choice == 'Q'):\n",
+ " print \"Input stopped\"\n",
+ " else:\n",
+ " printMessage()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Q to quit, any other character to continue: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "w\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " This function called 1 times\n",
+ "Enter Q to quit, any other character to continue: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "t\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " This function called 2 times\n",
+ "Enter Q to quit, any other character to continue: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "u\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " This function called 3 times\n",
+ "Enter Q to quit, any other character to continue: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Q\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Input stopped\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.8, page no. 189"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "static local variables\n",
+ "note: there is no static keyword in python so we will use it as global variable\n",
+ "only.\n",
+ "\"\"\"\n",
+ "\n",
+ "times = 0\n",
+ "def printMessage():\n",
+ " global times\n",
+ " times += 1\n",
+ " print \"This function called \", times, \" times\"\n",
+ "\n",
+ "\n",
+ "choice = '1'\n",
+ "while(choice != 'Q'):\n",
+ " print \"Enter Q to quit, any other character to continue: \",\n",
+ " choice = raw_input()\n",
+ " if (choice == 'Q'):\n",
+ " print \"Input stopped\"\n",
+ " else:\n",
+ " printMessage()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Q to quit, any other character to continue: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "w\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " This function called 1 times\n",
+ "Enter Q to quit, any other character to continue: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "r\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " This function called 2 times\n",
+ "Enter Q to quit, any other character to continue: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "t\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " This function called 3 times\n",
+ "Enter Q to quit, any other character to continue: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Q\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Input stopped\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.9, page no. 190"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "sending info. to a function\n",
+ "\"\"\"\n",
+ "\n",
+ "def printMessage():\n",
+ " print \"You inputted \", str\n",
+ "\n",
+ "print \"Enter a string: \",\n",
+ "str = raw_input()\n",
+ "printMessage()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a string: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Jeff\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " You inputted Jeff\n"
+ ]
+ }
+ ],
+ "prompt_number": 22
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.10, page no. 191"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "passing argument by value\n",
+ "\"\"\"\n",
+ "\n",
+ "def printMessage(s):\n",
+ " print \"You inputted \", s\n",
+ "\n",
+ "print \"Enter a string: \",\n",
+ "str = raw_input()\n",
+ "printMessage(str)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a string: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Jeff\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " You inputted Jeff\n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.11, page no. 193"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "multiple function arguments\n",
+ "\"\"\"\n",
+ "\n",
+ "def printMessage(firstName, lastName):\n",
+ " print \"Your name is \", firstName, \" \", lastName\n",
+ "\n",
+ "print \"Enter first name:\",\n",
+ "name1 = raw_input()\n",
+ "print \"Enter last name:\",\n",
+ "name2 = raw_input()\n",
+ "printMessage(name1, name2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first name:"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Jeff\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter last name:"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Kent\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Your name is Jeff Kent\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.12, page no. 194"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "multiple function arguments\n",
+ "note: in Python even if the arguments are reversed, it won't give an error as \n",
+ "we do not have to specipy the data type explicitly. The meaning or the context\n",
+ "of the output may change.\n",
+ "\"\"\"\n",
+ "\n",
+ "def printMessage(thename, theage):\n",
+ " print \"Your name is \", thename, \" and your age is\", theage\n",
+ "\n",
+ "print \"Enter name:\",\n",
+ "name = raw_input()\n",
+ "print \"Enter age:\",\n",
+ "age = int(raw_input())\n",
+ "printMessage(name, age)\n",
+ "printMessage(age, name)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name:"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Jeff\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter age:"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "34\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Your name is Jeff and your age is 34\n",
+ "Your name is 34 and your age is Jeff\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.13, page no. 195"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "passing argument by reference\n",
+ "\"\"\"\n",
+ "\n",
+ "def doubleIt(x):\n",
+ " print \"The number to be doubled is \", x\n",
+ " x *= 2\n",
+ " print \"The number doubled in doubleIt is \", x\n",
+ "\n",
+ "print \"Enter number: \",\n",
+ "num = int(raw_input())\n",
+ "doubleIt(num)\n",
+ "print \"The number doubled outside the function is \", num,"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "34\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " The number to be doubled is 34\n",
+ "The number doubled in doubleIt is 68\n",
+ "The number doubled outside the function is 34\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.14, page no. 196"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "pass by reference (address)\n",
+ "note: & sign is used in c++ to access a varibale by its address. There is no\n",
+ "such way or it is not a good practice to implement it. We will do the same in \n",
+ "some other way(there are multiple ways to achieve the same result)\n",
+ "\"\"\"\n",
+ "\n",
+ "li = [0]\n",
+ "def doubleIt():\n",
+ " print \"The number to be doubled is \", li[0]\n",
+ " li[0] *= 2\n",
+ " print \"The number doubled in doubleIt is \", li[0]\n",
+ " \n",
+ "print \"Enter number: \",\n",
+ "num = int(raw_input())\n",
+ "li[0] = num\n",
+ "doubleIt()\n",
+ "print \"The number doubled outside the function is \", li[0]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " The number to be doubled is 5\n",
+ "The number doubled in doubleIt is 10\n",
+ "The number doubled outside the function is 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.15, page no. 197"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "function to add two numbers\n",
+ "note: we are doing it in a differnt way then the one given in book\n",
+ "\"\"\"\n",
+ "\n",
+ "z = []\n",
+ "def addNumbers (a, b):\n",
+ " z.append(a + b)\n",
+ "\n",
+ "\n",
+ "print \"Enter first number: \",\n",
+ "firstNum = int(raw_input())\n",
+ "print \"Enter second number: \",\n",
+ "secondNum = int(raw_input())\n",
+ "addNumbers(firstNum, secondNum)\n",
+ "print firstNum, \" + \", secondNum, \" = \", z[0]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter second number: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 3 + 4 = 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 9.16, page no. 199"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "returning value from function\n",
+ "\"\"\"\n",
+ "\n",
+ "def addNumbers(x, y):\n",
+ " return x + y\n",
+ "\n",
+ "print \"Enter first number: \",\n",
+ "firstNum = int(raw_input())\n",
+ "print \"Enter second number: \",\n",
+ "secondNum = int(raw_input())\n",
+ "sum = addNumbers(firstNum, secondNum)\n",
+ "print firstNum, \" + \", secondNum, \" = \", sum\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter second number: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 4 + 5 = 9\n"
+ ]
+ }
+ ],
+ "prompt_number": 24
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file