{ "metadata": { "name": "", "signature": "sha256:59d989e4d68b99137fb5255fe771e42a62c5ce35100304584930ea0c003ea2ac" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 07 : User Defined Functions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 1, page no.5.49" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to demonstrate Local and Global Declaration \n", "\n", "#variable declaration globally\n", "i=0 \n", "print \"Value of i in main\", i\n", "\n", " #define function\n", "def f1(): \n", " k=0\n", " i=50\n", " print \"Value of i after function call\",i\n", " \n", "#Function Call\n", "f1() \n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Value of i in main 0\n", "Value of i after function call 50\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 2, page no: 5.50" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to demonstrate usage of local and global variable\n", "\n", "for i in range(0,3):\n", " print \"Value of i is:\", i\n", " \n", "i=590\n", "print \"The Value of i is:\", i \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Value of i is: 0\n", "Value of i is: 1\n", "Value of i is: 2\n", "The Value of i is: 590\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 3, Page no.5.52" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to call function without parameters\n", "def message():\n", " print \"Main Message\"\n", " \n", "print \"Function Message\"\n", "\n", "message()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Function Message\n", "Main Message\n" ] } ], "prompt_number": 24 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 4, Page No.5.52" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to call function with parameter\n", "\n", "#Defining function\n", "def add(x,y):\n", " z=0\n", " z=x+y\n", " return z\n", "\n", "k=add(10,20)\n", "\n", "#Result\n", "print \"The Value of k is \", k" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The Value of k is 30\n" ] } ], "prompt_number": 25 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 5, page no.5.53" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to write prototype of the function\n", "\n", "k=0\n", "\n", "#Defining in function\n", "def f1(k):\n", " k=k+100\n", " return k\n", "\n", "#Result\n", "print \"The value of i before call: \", k\n", "\n", "print \"The Value of i after call: \",(f1(k))\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of i before call: 0\n", "The Value of i after call: 100\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: 6, page no. 5.54" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to write prototype definition in main function\n", "\n", "def fun(k):\n", " k=k+10\n", " return k\n", "\n", "i=0\n", "\n", "#Result\n", "print \"The i value before call %d\\n\"%(i)\n", "print \"The i value after call %d\\n\"%fun(i)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The i value before call 0\n", "\n", "The i value after call 10\n", "\n" ] } ], "prompt_number": 4 } ], "metadata": {} } ] }