# HG changeset patch # User Anoop Jacob Thomas # Date 1289390982 -19800 # Node ID 1aa91f8d8d59c0a859c6bbc15aa0898e20815b4f # Parent 79a7ca3073d4bd51addb4f889ed001213255d8d1 added script getting started with functions. diff -r 79a7ca3073d4 -r 1aa91f8d8d59 getting-started-with-functions/script.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/getting-started-with-functions/script.rst Wed Nov 10 17:39:42 2010 +0530 @@ -0,0 +1,291 @@ +.. Objectives +.. ---------- + +.. 8.1 LO: getting started with functions (3) + +.. At the end of this tutorial, you will be able to + +.. 1. define function +.. #. define functions with arguments +.. #. learn about docstrings +.. #. learn about return values +.. can return multiple values +.. #. read code + + +.. Prerequisites +.. ------------- + +.. 1. should have ``ipython`` installed. +.. #. getting started with ``ipython``. + + +.. Author : Anoop Jacob Thomas + Internal Reviewer : + External Reviewer : + Checklist OK? : [2010-10-05] + + +============================== +Getting started with functions +============================== + +{{{ show welcome slide }}} + +Hello and welcome to the tutorial getting started with functions. + +{{{ switch to next slide, outline slide }}} + +In this tutorial we will learn about functions in python, how to +define functions, arguments to functions, docstrings, and function +return value. + +{{{ switch to next slide, Function }}} + +While writing code, we always want to reduce the number of lines of +code and functions is a way of reusing the code. Thus the same lines +of code can be used again and again. A function is a portion of code +within a larger program that performs a specific task and is +relatively independent of the remaining code. Now let us get more +familiar with functions, + +{{{ switch to next slide, f(x) a mathematical function }}} + +Consider a mathematical function f(x) = x square. Here x is a variable +and with different values of x the value of function will change. When +x is one f(1) will return the value 1 and f(2) will return us the +value 4. Let us now see how to define the function f(x) in python. + +{{{ switch to next slide, define f(x) in Python }}} + +In your Ipython interpreter type the following, +:: + + def f(x): + return x*x + +Well that defined the function, so before learning what we did let us +see if it returns the expected values, try, +:: + + f(1) + f(2) + +Yes, it returned 1 and 2 respectively. And now let us see what we did, +we wrote two lines. The first line ``def f(x)`` is used to define the +name and the parameters to the function. ``def`` is a keyword and +``f`` is the name of the function and ``x`` the parameter of the +function. + +{{{ switch to next slide, problem statement 1 }}} + +%% 1 %% Write a python function named cube which computes the cube of + a given number n. + +Pause here and try to solve the problem yourself. + +{{{ switch to next slide, solution }}} + +The problem can be solved as, +:: + + def cube(n): + return n**3 + +And now let us see how to write functions without arguments. + +{{{ switch to next slide, greet function }}} + +let us define a new function called ``greet`` which will print ``Hello +World``. +:: + + def greet(): + print "Hello World!" + +now try calling the function, +:: + + greet() + +Well that is a function which takes no arguments. Also note that it is +not mandatory for a function to return values. The function ``greet`` +neither takes any argument nor returns any value. + +Now let us see how to write functions with more than one argument. + +{{{ switch to next slide, exercise 2 }}} + +%% 2 %% Write a python function named ``avg`` which computes the + average of ``a`` and ``b``. + +Pause here and try to solve the problem yourself. + +{{{ switch to next slide, solution 2 }}} + +The problem can be solved as, +:: + + def avg(a,b): + return (a + b)/2 + +Thus if we want a function to accept more arguments, we just list them +separated with a comma between the parenthesis after the function name +in the ``def`` line. + +{{{ switch to next slide, docstring }}} + +It is always a good practice to document the code that we write, and +for a function we define we should write an abstract of what the +function does, and that is called a docstring. Let us modify the +function ``avg`` and add docstring to it. Do the following, +:: + + def avg(a,b): + """ avg takes two numbers as input (a & b), and + returns the average of a and b""" + return (a+b)/2 + +Note that docstrings are entered in the immediate line after the +function definition and put as a triple quoted string. And here as far +as the code functionality is concerned, we didn't do anything. We just +added an abstract of what the function does. + +Now try this in the ipython interpreter. +:: + + avg? + +It displays the docstring as we gave it. Thus docstring is a good way +of documenting the function we write. + +Try to do this, +:: + + f? + +It doesn't have a docstring associated with it. Also we cannot infer +anything from the function name, and thus we are forced to read the +code to understand anything about the function. + +{{{ switch to next slide, exercise 3 }}} + +%% 3 %% Add docstring to the function f. + +Pause here and try to do it yourself. + +{{{ switch to next slide, solution }}} + +We need to define the function again to add docstring to the function +``f`` and we do it as, +:: + + def f(x): + """Accepts a number x as argument and, + returns the square of the number x.""" + return x*x + +{{{ switch to next slide, exercise 4 }}} + +%% 4 %% Write a python function named ``circle`` which returns the + area and perimeter of a circle given radius ``r``. + +Pause here and try to solve the problem yourself. + +{{{ switch to next slide, solution 4 }}} + +The problem requires us to return two values instead of one which we +were doing till now. We can solve the problem as, +:: + + def circle(r): + """returns area and perimeter of a circle given radius r""" + pi = 3.14 + area = pi * r * r + perimeter = 2 * pi * r + return area, perimeter + +A python function can return any number of values. There is no +restriction for it. + +Let us call the function ``circle`` as, +:: + + a, p = circle(6) + print a + print p + +Now we have done enough coding, let us do some code reading exercise, + +{{{ switch to next slide, what }}} + +What does the function ``what`` do? + +.. def what( n ): +.. if n < 0: n = -n +.. while n > 0: +.. if n % 2 == 1: +.. return False +.. n /= 10 +.. return True + +Pause here and try to figure out what the function ``what`` does. + +{{{ switch to next slide, even_digits }}} + +.. def even_digits( n ): +.. """returns True if all the digits of number n is even +.. returns False if all the digits of number n is not even""" +.. if n < 0: n = -n +.. while n > 0: +.. if n % 2 == 1: +.. return False +.. n /= 10 +.. return True + +The function returns ``True`` if all the digits of the number ``n`` +are even, otherwise it returns ``False``. + +Now one more code reading exercise, + +{{{ switch to next slide, what }}} + +What does the function ``what`` do? + +.. def what( n ): +.. i = 1 +.. while i * i < n: +.. i += 1 +.. return i * i == n, i + +Pause here and try to figure out what the function ``what`` does. + +{{{ switch to next slide, is_perfect_square }}} + +.. def is_perfect_square( n ): +.. """returns True and square root of n, if n is a perfect square, +.. otherwise returns False and the square root of the +.. next perfect square""" +.. i = 1 +.. while i * i < n: +.. i += 1 +.. return i * i == n, i + + +The function returns ``True`` and the square root of ``n`` if n is a +perfect square, otherwise it returns ``False`` and the square root of +the next perfect square. + +This brings us to the end of this tutorial, in this tutorial we covered + +{{{ switch to next slide, summary }}} + +- Functions in Python +- Passing parameters to a function +- Returning values from a function + +We also did few code reading exercises. + +{{{ switch to next slide, Thank you }}} + +Thank you! diff -r 79a7ca3073d4 -r 1aa91f8d8d59 outline_and_allocations.rst --- a/outline_and_allocations.rst Wed Nov 10 17:25:18 2010 +0530 +++ b/outline_and_allocations.rst Wed Nov 10 17:39:42 2010 +0530 @@ -447,7 +447,7 @@ 8 Module 8: Python Language: Advanced (16) ========================================== -8.1 LO: getting started with functions (3) [nishanth] +8.1 LO: getting started with functions (3) [anoop] ----------------------------------------------------- * defining function * arguments diff -r 79a7ca3073d4 -r 1aa91f8d8d59 progress.org --- a/progress.org Wed Nov 10 17:25:18 2010 +0530 +++ b/progress.org Wed Nov 10 17:39:42 2010 +0530 @@ -46,7 +46,7 @@ | 7.5 LO: | sets | 2 | Nishanth | Punch (Done) | | | 7.6 LO: | Assessment | 3 | Amit | | | |---------+----------------------------------------+-------+----------+-----------------+-----------| -| 8.1 LO: | getting started with functions | 3 | Nishanth | | | +| 8.1 LO: | getting started with functions | 3 | Anoop | | | | 8.2 LO: | advanced features of functions | 3 | Punch | Anoop (Done) | | | 8.3 LO: | using python modules | 3 | Anoop | Punch (Done) | | | 8.4 LO: | writing python scripts | 2 | Nishanth | | |