"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Recieve values from user defined function without passing any \n",
"#value through main().\n",
"\n",
"import sys\n",
"\n",
"#Function definition\n",
"def sum1():\n",
" x = int(raw_input(\"Enter Three Numbers : \"))\n",
" y = int(raw_input(\"Enter Three Numbers : \"))\n",
" z = int(raw_input(\"Enter Three Numbers : \"))\n",
" return x+y+z\n",
"\n",
"#Function call\n",
"s = sum1()\n",
"\n",
"#Result\n",
"sys.stdout.write(\"Sum = %d\"%(s))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Three Numbers : 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Three Numbers : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Three Numbers : 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sum = 12"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 10.14, Page number: 333
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Return value in the form of address.\n",
"\n",
"import sys\n",
"\n",
"#Function definition\n",
"def sum1():\n",
" x = int(raw_input(\"Enter Three Values : \"))\n",
" y = int(raw_input(\"Enter Three Values : \"))\n",
" z = int(raw_input(\"Enter Three Values : \"))\n",
" k = x + y + z\n",
" return k\n",
"\n",
"#There is no pointer concept in python\n",
"\n",
"#Function call\n",
"s = sum1()\n",
"\n",
"#Result\n",
"sys.stdout.write(\"Sum = %d\"%(s))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Three Values : 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Three Values : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Three Values : 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sum = 12"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 10.15, Page number: 334
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Call by value\n",
"\n",
"import sys\n",
"\n",
"#Function Definition\n",
"def change(a,b):\n",
" k = a\n",
" a = b\n",
" b = k\n",
" sys.stdout.write(\"\\nIn Change() X = %d Y = %d\"%(a,b))\n",
" \n",
"#Variable Initialization\n",
"x = int(raw_input(\"Enter Values of X & Y : \"))\n",
"y = int(raw_input(\"Enter Values of X & Y : \"))\n",
"\n",
"#Function call\n",
"change(x,y)\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nIn main() X = %d Y = %d\"%(x,y))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values of X & Y : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values of X & Y : 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"In Change() X = 4 Y = 5\n",
"In main() X = 5 Y = 4"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 10.16, Page number: 335
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Call by reference \n",
"\n",
"import sys\n",
"\n",
"#Function Definition\n",
"def change(a,b):\n",
" k = a\n",
" a = b\n",
" b = k\n",
" sys.stdout.write(\"\\nIn Change() X = %d Y = %d\"%(a,b))\n",
" \n",
"#There is no pointer concept in python\n",
"\n",
"#Variable Initialization\n",
"x = int(raw_input(\"Enter Values of X & Y : \"))\n",
"y = int(raw_input(\"Enter Values of X & Y : \"))\n",
"\n",
"#Function call\n",
"change(x,y)\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nIn main() X = %d Y = %d\"%(y,x))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values of X & Y : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values of X & Y : 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"In Change() X = 4 Y = 5\n",
"In main() X = 4 Y = 5"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 10.17, Page number: 336
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Return by reference\n",
"\n",
"import sys\n",
"\n",
"#Function Definition\n",
"def change(a,b):\n",
" c = a + b\n",
" d = a - b\n",
" return c,d\n",
"\n",
"#Variable Initialization\n",
"x = int(raw_input(\"Enter Values of X & Y : \"))\n",
"y = int(raw_input(\"Enter Values of X & Y : \"))\n",
"\n",
"#Function call\n",
"add,sub = change(x,y)\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nAddition : %d\"%(add))\n",
"sys.stdout.write(\"\\nSubtraction : %d\"%(sub))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values of X & Y : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Values of X & Y : 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Addition : 9\n",
"Subtraction : 1"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 10.18, Page number: 337
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Call by value and reference\n",
"\n",
"k = 0\n",
"m = 0\n",
"\n",
"#Function Definition\n",
"def other(k,m):\n",
" sys.stdout.write(\"\\nAddress of k & m in other() : %u %u\"%(id(k),id(m)))\n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\nAddress of k & m in main() : %u %u\"%(id(k),id(m)))\n",
"\n",
"#Function call\n",
"other(k,m)\n",
"\n",
"#there is no pointer concept in python and it uses value tagged method in data storage\n",
"#instead of addressing the memory location, values of same variables are tagged together"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Address of k & m in main() : 30922996 30922996\n",
"Address of k & m in other() : 30922996 30922996"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Sum of 1 to 5 numbers using recursion\n",
"\n",
"import sys\n",
"global s\n",
"\n",
"s = 0\n",
"#Function definition\n",
"def main(x,s):\n",
" s = s + x\n",
" sys.stdout.write(\"\\nx = %d s = %d\"%(x,s))\n",
" if x == 5:\n",
" return\n",
" x += 1\n",
" main(x,s)\n",
" \n",
"main(1,s)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"x = 1 s = 1\n",
"x = 2 s = 3\n",
"x = 3 s = 6\n",
"x = 4 s = 10\n",
"x = 5 s = 15"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 10.46, Page number: 365
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Calculate triangular number of a given number with recursion function method\n",
"\n",
"import sys\n",
"\n",
"#Function definition\n",
"def tri_num(m):\n",
" f = 0\n",
" if m == 0:\n",
" return f\n",
" else:\n",
" f = f + m + tri_num(m-1)\n",
" return f\n",
"\n",
"#Variable Initialization\n",
"n = int(raw_input(\"Enter a Number : \"))\n",
"\n",
"#Function call\n",
"t = tri_num(n)\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nTriangular number of %d is %d\"%(n,t))\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Number : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Triangular number of 5 is 15"
]
}
],
"prompt_number": 31
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 10.47, Page number: 366
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the given string using recursion\n",
"\n",
"import sys\n",
"import os\n",
"global x\n",
"\n",
"#Variable Initialization\n",
"x = 0\n",
"str1 = \"Have a Good Day\"\n",
"\n",
"#Function Definition\n",
"def main(x):\n",
" if x == len(str1): #There is no null terminating character in python string\n",
" return\n",
" else:\n",
" if str1[x] == 'H':\n",
" os.system('cls')\n",
" sys.stdout.write(\"%c\"%(str1[x]))\n",
" else:\n",
" sys.stdout.write(\"%c\"%(str1[x]))\n",
" x += 1\n",
" main(x)\n",
" \n",
"#Function call\n",
"main(x)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Have a Good Day"
]
}
],
"prompt_number": 47
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 10.48, Page number: 367
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the given string 10 times using recursion\n",
"\n",
"import sys\n",
"import os\n",
"\n",
"#Function definition\n",
"def main(x):\n",
" sys.stdout.write(\"\\n%.2d] %s\"%(x,str1))\n",
" x += 1\n",
" if x == 11:\n",
" return\n",
" else:\n",
" if x == 1:\n",
" os.system('cls')\n",
" main(x)\n",
" else:\n",
" main(x)\n",
" \n",
" \n",
"#Variable Initialization\n",
"x = 0\n",
"str1 = \"Have a Good Day\"\n",
"\n",
"#Function call\n",
"main(x)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"00] Have a Good Day\n",
"01] Have a Good Day"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"02] Have a Good Day\n",
"03] Have a Good Day\n",
"04] Have a Good Day\n",
"05] Have a Good Day\n",
"06] Have a Good Day\n",
"07] Have a Good Day\n",
"08] Have a Good Day\n",
"09] Have a Good Day\n",
"10] Have a Good Day"
]
}
],
"prompt_number": 51
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 10.49, Page number: 368
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Factorial using recursive function.\n",
"\n",
"import sys\n",
"\n",
"#Function Definition\n",
"def fact(m):\n",
" f = 1\n",
" if m == 1:\n",
" return 1\n",
" else:\n",
" f = m * fact(m-1)\n",
" return f\n",
" \n",
"#Variable Initialization\n",
"x = int(raw_input(\"Enter a Number : \"))\n",
"\n",
"#Function call\n",
"f = fact (x)\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nFactorial of %d is %d\"%(x,f))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Number : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Factorial of 5 is 120"
]
}
],
"prompt_number": 32
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 10.50, Page number: 369
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display address of user defined function\n",
"\n",
"import sys\n",
"\n",
"#Function definition\n",
"def show():\n",
" sys.stdout.write(\"\\nAddress of function show() is : \")\n",
" \n",
"#Function call\n",
"show()\n",
"\n",
"#Result\n",
"sys.stdout.write(\"%u\"%(id(show)))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Address of function show() is : 95041520"
]
}
],
"prompt_number": 54
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 10.51, Page number: 369
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Call function using pointer\n",
"\n",
"import sys\n",
"\n",
"#Function definition\n",
"def show():\n",
" sys.stdout.write(\"\\nAddress of function show() is : \")\n",
" \n",
"#There is no pointer concept in python\n",
"p = id(show)\n",
"show()\n",
"sys.stdout.write(\"%u\"%(id(show)))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Address of function show() is : 95041200"
]
}
],
"prompt_number": 55
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 10.52, Page number: 370
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the address of library function.\n",
"\n",
"import sys\n",
"import os\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nAddress of printf() is %u\"%(id(sys.stdout.write)))\n",
"sys.stdout.write(\"\\nAddress of scanf() is %u\"%(id(sys.stdout.read)))\n",
"sys.stdout.write(\"\\nAddress of clrscr() is %u\"%(id(os.system('cls'))))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Address of printf() is 60743848\n",
"Address of scanf() is 60743848\n",
"Address of clrscr() is 4774132"
]
}
],
"prompt_number": 57
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"