{ "metadata": { "name": "", "signature": "sha256:2bed6e40cd55f716f11e909c76b0c924a863909239fcbbf975814260bb7b4531" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 15 : Pointers and Functions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Call By Value" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 1, Page no. 5.112" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Call by value\n", "\n", "def change(x,y):\n", " x=x+5\n", " y=y+10\n", " print \"In the function changes a&b is %d,%d\"%(x,y)\n", " \n", "a=10\n", "b=20\n", "\n", "print \"\\nBefore calling the function a&b is %d,%d\"%(a,b)\n", "\n", "change(a,b)\n", "print \"\\nAfter calling a&b is %d,%d\"%(a,b)\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Before calling the function a&b is 10,20\n", "In the function changes a&b is 15,30\n", "\n", "After calling a&b is 10,20\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 2, Page no.5.113" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#program to swap(interchange) the two given variable.\n", "\n", "def swap(x,y):\n", " \n", " x=x+y\n", " y=x-y\n", " x=x-y\n", " print \"In swap function x=%d, y=%d\"%(x,y)\n", " return y,x\n", "\n", "a=10\n", "b=20\n", "\n", "print \"Before swap a=%d, b=%d\"%(a,b)\n", "\n", "\n", "print \"\\nAfter the calling the swap function, a=%d,b=%d\"%swap(a,b)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Before swap a=10, b=20\n", "In swap function x=20, y=10\n", "\n", "After the calling the swap function, a=10,b=20\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Call By Reference" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 1, Page.No.5.114" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Call By Reference\n", "\n", "def change (x,y):\n", " x=x+5\n", " y=y+10\n", " print \"In the function change a&b is %d %d\"%(x,y)\n", " return x,y\n", "a=10\n", "b=20\n", "print \"\\nBefore calling the function a&b is %d %d\"%(a,b)\n", "\n", "\n", "print \"After calling the function a&b is %d %d\"%change(a,b)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Before calling the function a&b is 10 20\n", "In the function change a&b is 15 30\n", "After calling the function a&b is 15 30\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 2, Page No.5.115" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to swap(intercahnge) the value of variables.\n", "\n", "a=10\n", "b=20\n", "\n", "print \"\\nBefore swap a=%d, b=%d\"%(swap(a,b))\n", "\n", "def swap(x,y):\n", " x=x+y\n", " y=x-y\n", " x=x-y\n", " print\"\\nIn swap function x=%d,y=%d\"%(x,y)\n", " return y,x\n", "\n", "print \"\\nAfter calling swap function a=%d, b=%d\"%(b,a)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "In swap function x=20,y=10\n", "\n", "Before swap a=10, b=20\n", "\n", "After calling swap function a=20, b=10\n" ] } ], "prompt_number": 35 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 3, Page no. 5.115" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to interchange the value of variables using call by reference\n", "def exchange(m,n):\n", " t=m\n", " m=n\n", " n=t\n", " return n,m\n", "\n", "a,b=input(\"Enter two Numbers: \")\n", "\n", "print \"Before Exchange a=%d,b=%d\"%(a,b)\n", "\n", "\n", "print \"After Exchange a=%d, b=%d\"%(exchange(b,a))\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter two Numbers: 10,20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Before Exchange a=10,b=20\n", "After Exchange a=20, b=10\n" ] } ], "prompt_number": 3 } ], "metadata": {} } ] }