[Tutor] Focus on the functions [Was Re: if-else statements]
bgailer at alum.rpi.edu
Sun Oct 16 18:14:31 CEST 2005
At 08:11 AM 10/16/2005, Norman Silverstone wrote:
>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
>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.
1) did you ever use GOSUB in Basic or procedure or function in Pascal?
If so, same thing here.
a) as a way to set aside pieces of code `that might otherwise obscure
an algorithm. For example if I have a complex calculation to do inside
some loop it may make the program easier to read to put the
calculation in a function and call the function from w/in the loop.
b) the classical use of functions is to package code that might be
called from more than one place rather than writing the same code
c) in Python (different than Basic or Pascal) functions are objects that
can be passed around, allowing for things like dictionaries that
choose which function to use based on some input or condition.
d) in classes functions become methods, a way to evoke a behavior of
More information about the Tutor