diff options
Diffstat (limited to 'Let_us_C/chapter-5.ipynb')
-rw-r--r-- | Let_us_C/chapter-5.ipynb | 804 |
1 files changed, 804 insertions, 0 deletions
diff --git a/Let_us_C/chapter-5.ipynb b/Let_us_C/chapter-5.ipynb new file mode 100644 index 00000000..62f0970a --- /dev/null +++ b/Let_us_C/chapter-5.ipynb @@ -0,0 +1,804 @@ +{
+ "metadata": {
+ "name": "chapter-5.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 5: Functions & Pointers <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Message Function, Page number: 159<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate a simple function'''\n",
+ "\n",
+ "# function definition\n",
+ "def message():\n",
+ " print \"Smile, and the world smiles with you...\" \n",
+ "\n",
+ "message() #function call\n",
+ "print \"Cry, and you stop the monotony!\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Smile, and the world smiles with you...\n",
+ "Cry, and you stop the monotony!\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Calling More Than One Funnction , Page number: 159<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate a call to more than one function'''\n",
+ "\n",
+ "# function definitions\n",
+ "def italy():\n",
+ " print \"I am in italy\" \n",
+ " \n",
+ "def brazil():\n",
+ " print \"I am in brazil\" \n",
+ "\n",
+ "def argentina():\n",
+ " print \"I am in argentina\" \n",
+ " \n",
+ "\n",
+ "print \"I am in main\" \n",
+ "#function calls\n",
+ "italy()\n",
+ "brazil()\n",
+ "argentina()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I am in main\n",
+ "I am in italy\n",
+ "I am in brazil\n",
+ "I am in argentina\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Function Calling Function, Page number: 161<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate a call to more than one function'''\n",
+ "\n",
+ "# function definitions\n",
+ "def argentina():\n",
+ " print \"I am in argentina\" \n",
+ "\n",
+ "def brazil():\n",
+ " print \"I am in brazil\" \n",
+ " argentina() #function call\n",
+ "\n",
+ "def italy():\n",
+ " print \"I am in italy\" \n",
+ " brazil() #function call\n",
+ " print \"I am back in italy\" \n",
+ " \n",
+ "print \"I am in main\" \n",
+ "#function call\n",
+ "italy()\n",
+ "print \"I am finally back in main\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "I am in main\n",
+ "I am in italy\n",
+ "I am in brazil\n",
+ "I am in argentina\n",
+ "I am back in italy\n",
+ "I am finally back in main\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Calsum Function , Page number: 166<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate sending and receiving values between functions'''\n",
+ "\n",
+ "# function definition\n",
+ "def calsum ( x, y, z ):\n",
+ " d = x + y + z\n",
+ " return d\n",
+ "\n",
+ "#Input from user\n",
+ "#a,b,c = raw_input (\"Enter any three numbers: \").split()\n",
+ "print \"Enter any three numbers: \"\n",
+ "a = 10\n",
+ "b = 20\n",
+ "c = 30\n",
+ "print a,b,c\n",
+ "\n",
+ "#function call\n",
+ "s = calsum(a,b,c) \n",
+ "\n",
+ "#Result\n",
+ "print \"Sum = \", s \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any three numbers: \n",
+ "10 20 30\n",
+ "Sum = 60\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Return Statement , Page number: 169<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate that there is no restriction on the number of return\n",
+ "statements that may be present in a function.\n",
+ "Also, the return statement need not always be present at the end of the called\n",
+ "function.'''\n",
+ "\n",
+ "# function definition\n",
+ "def fun():\n",
+ " #Input from user\n",
+ " #ch = raw_input (\"Enter any alphabet: \")\n",
+ " print \"Enter any alphabet: \"\n",
+ " ch = 'a'\n",
+ " print ch\n",
+ " ch = ord(ch)\n",
+ " if ch >= 65 and ch <= 90:\n",
+ " return ascii(ch)\n",
+ " else:\n",
+ " return ascii( ch + 32 )\n",
+ "\n",
+ "#function call\n",
+ "ch = fun()\n",
+ "\n",
+ "#Result\n",
+ "print ch\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Formal Arguments, Page number: 170<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate that If the value of a formal argument is\n",
+ "changed in the called function, the corresponding\n",
+ "change does not take place in the calling function.'''\n",
+ "\n",
+ "# function definition\n",
+ "def fun(b):\n",
+ " b = 60\n",
+ " print b \n",
+ "\n",
+ "a = 30\n",
+ "fun(a) #function call\n",
+ "print a"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "60\n",
+ "30\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Scope Rule of Functions , Page number: 171<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate Scope Rule of Functions'''\n",
+ "\n",
+ "# function definition\n",
+ "def display(j):\n",
+ " k = 35\n",
+ " print j\n",
+ " print k\n",
+ "\n",
+ "i = 20 \n",
+ "display(i) #function call\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "20\n",
+ "35\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Square Function , Page number: 176<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to find out square of a number using a function.'''\n",
+ "\n",
+ "# function definition\n",
+ "def square(x):\n",
+ " y = x * x\n",
+ " return y\n",
+ "\n",
+ "#Input from user\n",
+ "#a = raw_input(\"Enter any number: \")\n",
+ "print \"Enter any number: \"\n",
+ "a = 4.5\n",
+ "print a\n",
+ "\n",
+ "b = square(a) #function call\n",
+ "\n",
+ "#Result\n",
+ "print \"Square of %f is %f\" %( a, b )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number: \n",
+ "4.5\n",
+ "Square of 4.500000 is 20.250000\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Void Function , Page number: 177<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate a called function without returning any value using\n",
+ "the keyword void.'''\n",
+ "\n",
+ "# function definition\n",
+ "def gospel(): # void function\n",
+ " print \"Viruses are electronic bandits...\" \n",
+ " print \"who eat nuggets of information...\" \n",
+ " print \"and chunks of bytes...\" \n",
+ " print \"when you least expect...\" \n",
+ "\n",
+ "gospel() # function call"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Viruses are electronic bandits...\n",
+ "who eat nuggets of information...\n",
+ "and chunks of bytes...\n",
+ "when you least expect...\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Address of a Variable , Page number: 180<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to print the address of a variable'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 3\n",
+ "\n",
+ "#Result\n",
+ "print \"Address of i = \" , id(i) #printing address\n",
+ "print \"Value of i = \", i \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of i = 30301288\n",
+ "Value of i = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Pointer Relations , Page number: 182<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program that demonstrates the relationships between pointers , variables\n",
+ "and addresses'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 3 \n",
+ "j = id(i) # address of variable 'i'\n",
+ "\n",
+ "#Result\n",
+ "print \"Address of i = \", id(i) \n",
+ "print \"Address of i = \", j \n",
+ "print \"Address of j = \", id(j)\n",
+ "print \"Value of j = \", j \n",
+ "print \"Value of i = \", i \n",
+ "print \"Value of i = \", i \n",
+ "print \"Value of i = \", i \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of i = 30301288\n",
+ "Address of i = 30301288\n",
+ "Address of j = 134133200\n",
+ "Value of j = 30301288\n",
+ "Value of i = 3\n",
+ "Value of i = 3\n",
+ "Value of i = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Pointer to Another Pointer , Page number: 184<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program that demonstrates a pointer that\n",
+ "points to another pointer'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "i = 3\n",
+ "j = id(i) # address of i\n",
+ "k = id(j) # address of j\n",
+ "\n",
+ "#Result\n",
+ "print \"Address of i = \", id(i) \n",
+ "print \"Address of i = \", j \n",
+ "print \"Address of i = \", j \n",
+ "print \"Address of j = \", id(j) \n",
+ "print \"Address of j = \", k \n",
+ "print \"Address of k = \", id(k)\n",
+ "print \"Value of j = \", j \n",
+ "print \"Value of k = \", k \n",
+ "print \"Value of i = \", i \n",
+ "print \"Value of i = \", i \n",
+ "print \"Value of i = \", i \n",
+ "print \"Value of i = \", i "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of i = 30301288\n",
+ "Address of i = 30301288\n",
+ "Address of i = 30301288\n",
+ "Address of j = 134132944\n",
+ "Address of j = 134132944\n",
+ "Address of k = 134133200\n",
+ "Value of j = 30301288\n",
+ "Value of k = 134132944\n",
+ "Value of i = 3\n",
+ "Value of i = 3\n",
+ "Value of i = 3\n",
+ "Value of i = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Call By Value , Page number: 186<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate the \u2018Call by Value\u2019 method of passing arguments'''\n",
+ "\n",
+ "# function definition\n",
+ "def swapv (x,y):\n",
+ " x,y=y,x\n",
+ " print \"x = %d y = %d\" %( x, y )\n",
+ "\n",
+ "#Variable declaration\n",
+ "a = 10\n",
+ "b = 20\n",
+ "\n",
+ "swapv ( a, b ) # function call\n",
+ "\n",
+ "#Result\n",
+ "print \"a = %d b = %d\" %( a, b )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 20 y = 10\n",
+ "a = 10 b = 20\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Call By Reference , Page number: 187<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate the \u2018Call by Reference\u2019 method of passing arguments'''\n",
+ "\n",
+ "# function definition\n",
+ "def swapv (a,b):\n",
+ " a,b=b,a\n",
+ " return a,b\n",
+ "\n",
+ "#Variable declaration\n",
+ "a = 10\n",
+ "b = 20\n",
+ "\n",
+ "a,b = swapv ( a, b ) # function call\n",
+ "\n",
+ "#Result\n",
+ "print \"a = %d b = %d\" %( a, b )"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "a = 20 b = 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Area And Perimeter of a Circle , Page number: 188<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate how to use a call by reference to make a function\n",
+ "return more than one value at a time, which is not possible ordinarily.'''\n",
+ "\n",
+ "#Input from user\n",
+ "#radius = raw_input(\"Enter radius of a circle: \" )\n",
+ "print \"Enter radius of a circle: \"\n",
+ "radius = 5\n",
+ "print radius\n",
+ "\n",
+ "#Function definition\n",
+ "def areaperi ( r ):\n",
+ " a = 3.14 * r * r\n",
+ " p = 2 * 3.14 * r \n",
+ " return a,p\n",
+ "\n",
+ "area,perimeter = areaperi ( radius ) #function call\n",
+ "\n",
+ "\n",
+ "#Result\n",
+ "print \"Area = \", area \n",
+ "print \"Perimeter = \", perimeter "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter radius of a circle: \n",
+ "5\n",
+ "Area = 78.5\n",
+ "Perimeter = 31.4\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Iterative Factorial Function , Page number: 190<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate a non-recursive function for calculating\n",
+ "the factorial value of an integer'''\n",
+ "\n",
+ "# function definition\n",
+ "def factorial(x):\n",
+ " f = 1\n",
+ " for i in range(x,0,-1 ):\n",
+ " f = f * i\n",
+ " return f\n",
+ "\n",
+ "#Input from user\n",
+ "#a = raw_input( \"Enter any number: \" )\n",
+ "print \"Enter any number: \"\n",
+ "a = 6\n",
+ "print a\n",
+ "\n",
+ "fact = factorial(a) #function call\n",
+ "\n",
+ "#Result\n",
+ "print \"Factorial value = \", fact\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number: \n",
+ "6\n",
+ "Factorial value = 720\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Recursive Factorial Function, Page number: 191<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate a the recursive version of\n",
+ "the function to calculate the factorial value.'''\n",
+ "\n",
+ "# function definition\n",
+ "def rec(x):\n",
+ " if x == 1:\n",
+ " return 1\n",
+ " else:\n",
+ " f = x * rec ( x - 1 ) # calling the function\n",
+ " return f\n",
+ "\n",
+ "#Input from user\n",
+ "#a = raw_input( \"Enter any number: \" )\n",
+ "print \"Enter any number: \"\n",
+ "a = 5\n",
+ "print a\n",
+ "\n",
+ "fact = rec(a) #function call\n",
+ "\n",
+ "#Result\n",
+ "print \"Factorial value = \", fact \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number: \n",
+ "5\n",
+ "Factorial value = 120\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Recursion Stack , Page number: 195<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate the use of a stack for\n",
+ "implementing function calls'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "a = 5\n",
+ "b = 2\n",
+ "\n",
+ "#Function definition\n",
+ "def add ( i, j ):\n",
+ " s = i + j\n",
+ " return s\n",
+ "\n",
+ "c = add ( a, b ) # function call\n",
+ "\n",
+ "#Result\n",
+ "print \"sum = \", c "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "sum = 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |