getting-started-with-functions/script.rst
changeset 521 88a01948450d
parent 484 a1828587a617
equal deleted inserted replaced
520:8249ae9d570a 521:88a01948450d
    21 
    21 
    22      
    22      
    23 .. Author              : Anoop Jacob Thomas <anoop@fossee.in>
    23 .. Author              : Anoop Jacob Thomas <anoop@fossee.in>
    24    Internal Reviewer   : 
    24    Internal Reviewer   : 
    25    External Reviewer   :
    25    External Reviewer   :
    26    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    26    Language Reviewer   : Bhanukiran
       
    27    Checklist OK?       : <put date stamp here, not OK> [2010-10-05]
    27 
    28 
    28 
    29 
    29 ==============================
    30 ==============================
    30 Getting started with functions
    31 Getting started with functions
    31 ==============================
    32 ==============================
    32 
    33 
    33 {{{ show welcome slide }}}
    34 {{{ show welcome slide }}}
    34 
    35 
    35 Hello and welcome to the tutorial, getting started with functions.
    36 Hello and welcome to the tutorial getting started with functions.
    36 
    37 
    37 {{{ switch to next slide, outline slide }}}
    38 {{{ switch to next slide, outline slide }}}
    38 
    39 
    39 In this tutorial we will learn about functions in python, how to
    40 In this tutorial we will learn about functions in python, how to
    40 define functions, passing arguments to functions, docstrings, and
    41 define functions, arguments to functions, docstrings, and function
    41 function return value.
    42 return value.
    42 
    43 
    43 {{{ switch to next slide, Function }}}
    44 {{{ switch to next slide, Function }}}
    44 
    45 
    45 While writing code, we would like to reduce the number of lines of
    46 While writing code, we always want to reduce the number of lines of
    46 code and using functions is a way of reusing the code.  A function is
    47 code and functions is a way of reusing the code. Thus the same lines
    47 a portion of code within a larger program that performs a specific
    48 of code can be used as many times as needed. A function is a portion of code
    48 task and is relatively independent of the remaining code. Now let us
    49 within a larger program that performs a specific task and is
    49 get more familiar with functions,
    50 relatively independent of the remaining code. Now let us get more
       
    51 familiar with functions,
    50 
    52 
    51 {{{ switch to next slide, f(x) a mathematical function }}}
    53 {{{ switch to next slide, f(x) a mathematical function }}}
    52 
    54 
    53 Consider a mathematical function f(x) = x squared. Here x is a
    55 Consider a mathematical function f(x) = x square. Here x is a variable
    54 variable and with different values of x the value of function will
    56 and with different values of x the value of function will change. When
    55 change. When x is one f(1) will return the value 1 and f(2) will
    57 x is one f(1) will return the value 1 and f(2) will return us the
    56 return us the value 4. Let us now see how to define the function f(x)
    58 value 4. Let us now see how to define the function f(x) in python.
    57 in Python.
       
    58 
    59 
    59 {{{ switch to next slide, define f(x) in Python }}}
    60 {{{ switch to next slide, define f(x) in Python }}}
    60 
    61 
    61 In your Ipython interpreter type the following,
    62 In your Ipython interpreter type the following,
    62 ::
    63 ::
    63 
    64 
    64     def f(x):
    65     def f(x):
    65     	return x*x
    66     	return x*x
    66 
    67 
    67 Let us see, if the function ``f``, we defined, works as we expect. 
    68 Well that defined the function, so before learning what we did let us
       
    69 see if it returns the expected values, try,
    68 ::
    70 ::
    69 
    71 
    70     f(1)
    72     f(1)
    71     f(2)
    73     f(2)
    72 
    74 
    73 Yes, it returned 1 and 2 respectively. 
    75 Yes, it returned 1 and 4 respectively. And now let us see what we did.
    74 
    76 We wrote two lines: The first line ``def f(x)`` is used to define the
    75 Now, let us see what we did. The first line ``def f(x)`` is used to
    77 name and the parameters to the function and the second line is used to
    76 define the name and the parameters to the function. ``def`` is a
    78 fix what the function is supposed to return. ``def`` is a keyword and
    77 keyword and ``f`` is the name of the function and ``x`` the parameter
    79 ``f`` is the name of the function and ``x`` the parameter of the
    78 of the function.  
    80 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.
       
    83 
    81 
    84 {{{ switch to next slide, problem statement 1 }}}
    82 {{{ switch to next slide, problem statement 1 }}}
    85 
    83 
    86 %% 1 %% Write a python function named cube which computes the cube of
    84 %% 1 %% Write a python function named cube which computes the cube of
    87    a given number n.
    85    a given number n.
    94 ::
    92 ::
    95 
    93 
    96     def cube(n):
    94     def cube(n):
    97     	return n**3
    95     	return n**3
    98 
    96 
    99 Now let us see how to write functions without arguments.
    97 And now let us see how to write functions without arguments.
   100 
    98 
   101 {{{ switch to next slide, greet function }}}
    99 {{{ switch to next slide, greet function }}}
   102 
   100 
   103 Let us define a new function called ``greet`` which will print ``Hello
   101 let us define a new function called ``greet`` which will print ``Hello
   104 World``.
   102 World``.
   105 ::
   103 ::
   106 
   104 
   107     def greet():
   105     def greet():
   108     	print "Hello World!"
   106     	print "Hello World!"
   109 
   107 
   110 Now try calling the function,
   108 now try calling the function,
   111 ::
   109 ::
   112 
   110 
   113     greet()
   111     greet()
   114 
   112 
   115 Well that is a function which takes no arguments. Also note that it is
   113 Well that is a function which takes no arguments. Also note that it is
   125 
   123 
   126 Pause here and try to solve the problem yourself.
   124 Pause here and try to solve the problem yourself.
   127 
   125 
   128 {{{ switch to next slide, solution 2 }}}
   126 {{{ switch to next slide, solution 2 }}}
   129 
   127 
   130 The problem can be solved as shown,
   128 The problem can be solved as,
   131 ::
   129 ::
   132 
   130 
   133     def avg(a,b):
   131     def avg(a,b):
   134     	return (a + b)/2
   132     	return (a + b)/2
   135 
   133 
   136 Thus if we want a function to accept more arguments, we just list them
   134 Thus if we want a function to accept more arguments, we just list them
   137 separated with a comma between the parenthesis after the function name
   135 separated with a comma between the parenthesis after the function's name
   138 in the ``def`` line.
   136 in the ``def`` line.
   139 
   137 
   140 {{{ switch to next slide, docstring }}}
   138 {{{ switch to next slide, docstring }}}
   141 
   139 
   142 It is always a good practice to document the code that we write, and
   140 It is always a good practice to document the code that we write, and
   143 for a function we define we should write an abstract of what the
   141 for a function we define we should write an abstract of what the
   144 function does, and that is called a doc-string. Let us modify the
   142 function does, and that is called a docstring. Let us modify the
   145 function ``avg`` and add doc-string to it. Do the following,
   143 function ``avg`` and add docstring to it. Do the following,
   146 ::
   144 ::
   147 
   145 
   148     def avg(a,b):
   146     def avg(a,b):
   149         """ avg takes two numbers as input (a & b), and
   147         """ avg takes two numbers as input (a & b), and
   150 	returns the average of a and b"""
   148 	returns the average of a and b"""
   151 	return (a+b)/2
   149 	return (a+b)/2
   152 
   150 
   153 Note that doc-strings are entered in the immediate line after the
   151 Note that docstrings are entered in the immediate line after the
   154 function definition and put as a triple quoted string. And here as far
   152 function definition and put as a triple quoted string. And here as far
   155 as the code functionality is concerned, we didn't do anything. We just
   153 as the code functionality is concerned, we didn't do anything. We just
   156 added an abstract of what the function does.
   154 added an abstract of what the function does.
   157 
   155 
   158 Now try this in the ipython interpreter.
   156 Now try this in the ipython interpreter.
   168 
   166 
   169     f?
   167     f?
   170 
   168 
   171 It doesn't have a docstring associated with it. Also we cannot infer
   169 It doesn't have a docstring associated with it. Also we cannot infer
   172 anything from the function name, and thus we are forced to read the
   170 anything from the function name, and thus we are forced to read the
   173 code to understand anything about the function.
   171 code to understand about the function.
   174 
   172 
   175 {{{ switch to next slide, exercise 3 }}}
   173 {{{ switch to next slide, exercise 3 }}}
   176 
   174 
   177 %% 3 %% Add docstring to the function f.
   175 %% 3 %% Add docstring to the function f.
   178 
   176 
   236 Pause here and try to figure out what the function ``what`` does.
   234 Pause here and try to figure out what the function ``what`` does.
   237 
   235 
   238 {{{ switch to next slide, even_digits }}}
   236 {{{ switch to next slide, even_digits }}}
   239 
   237 
   240 .. def even_digits( n ):
   238 .. def even_digits( n ):
   241 ..    """returns True if all the digits of number n is even
   239 ..    """returns True if all the digits in the number n are even,
   242 ..    returns False if all the digits of number n is not even"""
   240 ..    returns False if all the digits in the number n are not even"""
   243 ..     if n < 0: n = -n
   241 ..     if n < 0: n = -n
   244 ..     while n > 0:
   242 ..     while n > 0:
   245 ..         if n % 2 == 1:
   243 ..         if n % 2 == 1:
   246 ..             return False
   244 ..             return False
   247 ..         n /= 10
   245 ..         n /= 10
   288 - Passing parameters to a function
   286 - Passing parameters to a function
   289 - Returning values from a function
   287 - Returning values from a function
   290 
   288 
   291 We also did few code reading exercises.
   289 We also did few code reading exercises.
   292 
   290 
   293 {{{ Show the "sponsored by FOSSEE" slide }}}
   291 {{{ switch to next slide, Thank you }}}
   294 
   292 
   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.
       
   298 Thank you!
   293 Thank you!