On 07/04/14 05:02, keith papa wrote:
Hi my name is keith and am new to python programming, Am learning python
for the first time with the help of coursera problem is am starting a
new topic call functions in python and am totally lost. can you please
help me understand how function work? why it use in python, what are the
rules of function etc.

Hi Keith,

Functions are basically mini programs that you can call from inside your program. You have probably used them already lots of times. For example things like print() (In Python v3), len() and range() are functions. There are many more that are built in to Python.

But Python also lets you define your own functions.
There are several reasons for doing this:
1) Functions make your code easier to understand. By bundling
   up a bunch of code that does something into a function and
   giving it a sensible name your code is easier to read.
2) Functions are reusable, they save you having to repeat
   the same set of code over and over in your program.
   Once you learn about modules you will also be able
   to reuse them across different programs.
3) Functions are easier to test. You can write a function
   and test it independently from the rest of your program.
   If your program is mostly made up of functions you can
   test all of the functions separately. Then testing the
   whole program is easier because you know that each
   small function works, so you only need to focus on
   the glue holding them together.

The last point will really only make sense to you once
you start writing bigger programs. And functions are
one of the keys to building bigger programs.


As to the rules of how to create functions. you follow a standard pattern.

def FunctionName(optional parameter list):
    code that does the function
    return some value(s) here

You then call it like this

aVariable = FunctionName(some values here)

Now you probably don't understand all of that (or any of it?)
But if you send a reply back to the list(hit Reply ALL)
highlighting the bits you don't get. Asking extra questions
where needed, then we can focus our replies where you
need them.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to