[Tutor] Focus on the functions [Was Re: if-else statements]

Pujo Aji ajikoe at gmail.com
Sun Oct 16 23:15:03 CEST 2005


Dear Norman,
 You can reuse function, this make your code more easy to handle.
Suppose you have code to calculate area of rectangle, but this time you like
to calculate 3 times:
 width = 3; length = 2
print 'area', width * length
  width = 1; length =32
print 'area', width * length
  width = 31; length = 22
print 'area', width * length
 it is better if you have function func_area
 def func_area(width,length):
 return func_area(width,length)
 then your code becomes:
 width = 3; length = 2
print 'area', func_area(width,length)
  width = 1; length =32
print 'area', func_area(width,length)
  width = 31; length = 22
print 'area', func_area(width ,length)
  And something happened that a new area formulation of rectangle is = width
* length * length (of course this is wrong, it is only an example that your
formula or code sometime needs to be changed), so instead of writing all of
this formulation one by one which prone to error, you can change only your
defined function func_area.
 def func_area(width,length):
 return width * length * length
  And you get the right result. The key here is that you can change your
code easily.
 The next day you create a code in another module for example new_module.py
then you would like to use your func_area which in old_module.py, then you
can just import and use it.
 in your new_module.py you can type:
import old_module
 print 'area of rectangle according old module is : ',
old_module.func_area(11,22)
 The key here is that you can reuse your function when you need it in other
module.
 Hope this help you understand a little bit the function of a function
 Cheers,
pujo
    Cheers,
pujo

 On 10/16/05, Norman Silverstone <norman at littletank.org> wrote:
>
>
> > I apologize to the list again for the noisy contentless ranting I made
> > yesterday, so I'll try making it up by doing concrete demos of a
> > function-focused approach. This will critique Zelle's Chapter Two and
> > Chapter Five, and see what things would look like if under a function
> > regime.
> >
> < snip>
>
> I am an elderly person somewhat physically handicapped with some
> experience programming in basic and pascal, many years ago. In order to
> keep myself mentally active I decided to have a look once again at
> programming and chose python as a good language to start with. So, I had
> a look at 'Dive into Python', bought a couple of books, 'Python
> Programming for the Absolute Beginner' and 'Learn to program Using
> Python'.
>
> All was well to start with until I came to the section on functions. I
> am finding it very difficult to grasp the reasoning behind the use of
> functions and how and when to use them. I keep trying to see if any of
> the snippets of programs that I have already come across are
> transferable into functions without a great deal of success. The problem
> appears to be that, and here I agree with the writer, functions are
> introduced as something on their own, well after manipulating strings,
> lists, dictionaries, tuples, loops etc are dealt with.
>
> Having studied the examples given here, I am beginning to get some
> understanding of what is going on. If only I could find many more
> examples like these, in simple English, I would be a very happy student.
>
> > Anyway, hope this helps!
>
> It certainly helps me.
>
> Norman
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051016/f115270d/attachment.html


More information about the Tutor mailing list