getting-started-with-functions/script.rst
changeset 463 192559461dcb
parent 444 1aa91f8d8d59
child 484 a1828587a617
equal deleted inserted replaced
462:3a1575a45152 463:192559461dcb
    30 Getting started with functions
    30 Getting started with functions
    31 ==============================
    31 ==============================
    32 
    32 
    33 {{{ show welcome slide }}}
    33 {{{ show welcome slide }}}
    34 
    34 
    35 Hello and welcome to the tutorial getting started with functions.
    35 Hello and welcome to the tutorial, getting started with functions.
    36 
    36 
    37 {{{ switch to next slide, outline slide }}}
    37 {{{ switch to next slide, outline slide }}}
    38 
    38 
    39 In this tutorial we will learn about functions in python, how to
    39 In this tutorial we will learn about functions in python, how to
    40 define functions, arguments to functions, docstrings, and function
    40 define functions, passing arguments to functions, docstrings, and
    41 return value.
    41 function return value.
    42 
    42 
    43 {{{ switch to next slide, Function }}}
    43 {{{ switch to next slide, Function }}}
    44 
    44 
    45 While writing code, we always want to reduce the number of lines of
    45 While writing code, we would like to reduce the number of lines of
    46 code and functions is a way of reusing the code. Thus the same lines
    46 code and using functions is a way of reusing the code.  A function is
    47 of code can be used again and again. A function is a portion of code
    47 a portion of code within a larger program that performs a specific
    48 within a larger program that performs a specific task and is
    48 task and is relatively independent of the remaining code. Now let us
    49 relatively independent of the remaining code. Now let us get more
    49 get more familiar with functions,
    50 familiar with functions,
       
    51 
    50 
    52 {{{ switch to next slide, f(x) a mathematical function }}}
    51 {{{ switch to next slide, f(x) a mathematical function }}}
    53 
    52 
    54 Consider a mathematical function f(x) = x square. Here x is a variable
    53 Consider a mathematical function f(x) = x squared. Here x is a
    55 and with different values of x the value of function will change. When
    54 variable and with different values of x the value of function will
    56 x is one f(1) will return the value 1 and f(2) will return us the
    55 change. When x is one f(1) will return the value 1 and f(2) will
    57 value 4. Let us now see how to define the function f(x) in python.
    56 return us the value 4. Let us now see how to define the function f(x)
       
    57 in Python.
    58 
    58 
    59 {{{ switch to next slide, define f(x) in Python }}}
    59 {{{ switch to next slide, define f(x) in Python }}}
    60 
    60 
    61 In your Ipython interpreter type the following,
    61 In your Ipython interpreter type the following,
    62 ::
    62 ::
    63 
    63 
    64     def f(x):
    64     def f(x):
    65     	return x*x
    65     	return x*x
    66 
    66 
    67 Well that defined the function, so before learning what we did let us
    67 Let us see, if the function ``f``, we defined, works as we expect. 
    68 see if it returns the expected values, try,
       
    69 ::
    68 ::
    70 
    69 
    71     f(1)
    70     f(1)
    72     f(2)
    71     f(2)
    73 
    72 
    74 Yes, it returned 1 and 2 respectively. And now let us see what we did,
    73 Yes, it returned 1 and 2 respectively. 
    75 we wrote two lines. The first line ``def f(x)`` is used to define the
    74 
    76 name and the parameters to the function. ``def`` is a keyword and
    75 Now, let us see what we did. The first line ``def f(x)`` is used to
    77 ``f`` is the name of the function and ``x`` the parameter of the
    76 define the name and the parameters to the function. ``def`` is a
    78 function.
    77 keyword and ``f`` is the name of the function and ``x`` the parameter
       
    78 of the function.  
       
    79 
       
    80 The second line is the body of the function. Incidentally, this
       
    81 function has a single line definition. Note that the body of the
       
    82 function is indented, just like any other code block, in Python.
    79 
    83 
    80 {{{ switch to next slide, problem statement 1 }}}
    84 {{{ switch to next slide, problem statement 1 }}}
    81 
    85 
    82 %% 1 %% Write a python function named cube which computes the cube of
    86 %% 1 %% Write a python function named cube which computes the cube of
    83    a given number n.
    87    a given number n.
    90 ::
    94 ::
    91 
    95 
    92     def cube(n):
    96     def cube(n):
    93     	return n**3
    97     	return n**3
    94 
    98 
    95 And now let us see how to write functions without arguments.
    99 Now let us see how to write functions without arguments.
    96 
   100 
    97 {{{ switch to next slide, greet function }}}
   101 {{{ switch to next slide, greet function }}}
    98 
   102 
    99 let us define a new function called ``greet`` which will print ``Hello
   103 Let us define a new function called ``greet`` which will print ``Hello
   100 World``.
   104 World``.
   101 ::
   105 ::
   102 
   106 
   103     def greet():
   107     def greet():
   104     	print "Hello World!"
   108     	print "Hello World!"
   105 
   109 
   106 now try calling the function,
   110 Now try calling the function,
   107 ::
   111 ::
   108 
   112 
   109     greet()
   113     greet()
   110 
   114 
   111 Well that is a function which takes no arguments. Also note that it is
   115 Well that is a function which takes no arguments. Also note that it is
   121 
   125 
   122 Pause here and try to solve the problem yourself.
   126 Pause here and try to solve the problem yourself.
   123 
   127 
   124 {{{ switch to next slide, solution 2 }}}
   128 {{{ switch to next slide, solution 2 }}}
   125 
   129 
   126 The problem can be solved as,
   130 The problem can be solved as shown,
   127 ::
   131 ::
   128 
   132 
   129     def avg(a,b):
   133     def avg(a,b):
   130     	return (a + b)/2
   134     	return (a + b)/2
   131 
   135 
   135 
   139 
   136 {{{ switch to next slide, docstring }}}
   140 {{{ switch to next slide, docstring }}}
   137 
   141 
   138 It is always a good practice to document the code that we write, and
   142 It is always a good practice to document the code that we write, and
   139 for a function we define we should write an abstract of what the
   143 for a function we define we should write an abstract of what the
   140 function does, and that is called a docstring. Let us modify the
   144 function does, and that is called a doc-string. Let us modify the
   141 function ``avg`` and add docstring to it. Do the following,
   145 function ``avg`` and add doc-string to it. Do the following,
   142 ::
   146 ::
   143 
   147 
   144     def avg(a,b):
   148     def avg(a,b):
   145         """ avg takes two numbers as input (a & b), and
   149         """ avg takes two numbers as input (a & b), and
   146 	returns the average of a and b"""
   150 	returns the average of a and b"""
   147 	return (a+b)/2
   151 	return (a+b)/2
   148 
   152 
   149 Note that docstrings are entered in the immediate line after the
   153 Note that doc-strings are entered in the immediate line after the
   150 function definition and put as a triple quoted string. And here as far
   154 function definition and put as a triple quoted string. And here as far
   151 as the code functionality is concerned, we didn't do anything. We just
   155 as the code functionality is concerned, we didn't do anything. We just
   152 added an abstract of what the function does.
   156 added an abstract of what the function does.
   153 
   157 
   154 Now try this in the ipython interpreter.
   158 Now try this in the ipython interpreter.
   284 - Passing parameters to a function
   288 - Passing parameters to a function
   285 - Returning values from a function
   289 - Returning values from a function
   286 
   290 
   287 We also did few code reading exercises.
   291 We also did few code reading exercises.
   288 
   292 
   289 {{{ switch to next slide, Thank you }}}
   293 {{{ Show the "sponsored by FOSSEE" slide }}}
   290 
   294 
       
   295 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   296 
       
   297 Hope you have enjoyed and found it useful.
   291 Thank you!
   298 Thank you!