author | asokan <asokan@fossee.in> |
Tue, 18 May 2010 15:40:17 +0530 | |
changeset 126 | 2eac725a5766 |
parent 122 | ed29ffa37cdc |
permissions | -rw-r--r-- |
106
3451e2b9002e
Started with functions second session day2
amit@shrike.aero.iitb.ac.in
parents:
diff
changeset
|
1 |
While we have talked about how you can do simple tasks in Python we haven't started to talk about how you can organize your code . One of the first techniques we use to break a task into relatively independent subtask . These can share data with other parts of the program . These code blocks clubbed together are called functions or subroutines . |
3451e2b9002e
Started with functions second session day2
amit@shrike.aero.iitb.ac.in
parents:
diff
changeset
|
2 |
|
3451e2b9002e
Started with functions second session day2
amit@shrike.aero.iitb.ac.in
parents:
diff
changeset
|
3 |
def keyword in Python is used to define a function. |
3451e2b9002e
Started with functions second session day2
amit@shrike.aero.iitb.ac.in
parents:
diff
changeset
|
4 |
Arguments are local to a function , i.e you can access the arguments only in a particular function in other places it shall raise a Name error . |
3451e2b9002e
Started with functions second session day2
amit@shrike.aero.iitb.ac.in
parents:
diff
changeset
|
5 |
|
3451e2b9002e
Started with functions second session day2
amit@shrike.aero.iitb.ac.in
parents:
diff
changeset
|
6 |
One of the great things about python is that a function can return multiple values . Essentialy it does it by packing the multiple values in a tuple . |
3451e2b9002e
Started with functions second session day2
amit@shrike.aero.iitb.ac.in
parents:
diff
changeset
|
7 |
|
3451e2b9002e
Started with functions second session day2
amit@shrike.aero.iitb.ac.in
parents:
diff
changeset
|
8 |
Lets look at how to write functions by writing one out . |
3451e2b9002e
Started with functions second session day2
amit@shrike.aero.iitb.ac.in
parents:
diff
changeset
|
9 |
|
3451e2b9002e
Started with functions second session day2
amit@shrike.aero.iitb.ac.in
parents:
diff
changeset
|
10 |
We have given the function the name signum . Essentially what it does is that based on whether the no is 0 , negative or positive , it returns 0 , -1 and +1 respectively . In this case it recieves value of the no in variable r . |
3451e2b9002e
Started with functions second session day2
amit@shrike.aero.iitb.ac.in
parents:
diff
changeset
|
11 |
|
122
ed29ffa37cdc
A minor edit to see if commits are working.
Puneeth Chaganti <punchagan@fossee.in>
parents:
106
diff
changeset
|
12 |
In the beginning of the function you can see a triple quoted string . This is called a docstring. You can write some documentation related to your function. In this you can have the function parameters and what it returns. A python function returns a value by using the keyword return. |
106
3451e2b9002e
Started with functions second session day2
amit@shrike.aero.iitb.ac.in
parents:
diff
changeset
|
13 |