A function is a peace of code and used to do some specific task on given data. Also we use functions to break our program to many sections or branches. I suggest you to read my tutorial on C functions. That'll explain the concept of functions very clearly. So in this tutorial I'm going to talk that how we can define and use functions in python. The structure for make a function is following. def function_name(): #Hear goes the code for function After you define it you can use it.
Thilan@ubuntu:~/programming$ cat tmp1.py
#!/usr/bin/env python
def myFunction():
  print ('I was printed by a function')

#now we can call it anywhere
myFunction()
#lets call it again
myFunction()

Thilan@ubuntu:~/programming$ python tmp1.py
I was printed by a function
I was printed by a function

Thilan@ubuntu:~/programming$ python3 tmp1.py
I was printed by a function
I was printed by a function
You may think why we use a function to do that. We could simply use print() function. Yes indeed . But since this is a basic demonstration of a function it doesn't do much. When we come to big and complex programs they are very helpful. Let's see some other examples of functions.

Functions with parameters

We can supply some arguments or parameters t a function and the function will do some task on those supplied data. Think about following example.
Thilan@ubuntu:~/programming$ cat tmp1.py
#!/usr/bin/env python
def addNumbers(a , b):
  print ('Sum of %d and %d is %d' %( a , b , a+ b))

addNumbers(2, 4)
addNumbers(3, 5)
Thilan@ubuntu:~/programming$ python tmp1.py
Sum of 2 and 4 is 6
Sum of 3 and 5 is 8

Thilan@ubuntu:~/programming$ python3 tmp1.py
Sum of 2 and 4 is 6
Sum of 3 and 5 is 8


You can see it's working on both versions. Now hear we have used a function called addNumbers. It takes two parameters as a and b. After it eil print out the value of a+b. If we use addNumbers(2, 4) , it will print 6 . If we supply 3 and 5 as parameters it gives us 8. So we don't want to tell what to do with those parameters again and again. It looks for function name and check if it's defined previously. After python looks what are the instructions those given when we define the function. Then it do those instructions on supplied data.

Functions with return values

We can give some data to a function. As well as a function can return processed data. Following example shows how we can do it.
Thilan@ubuntu:~/programming$ cat tmp1.py
#!/usr/bin/env python
def addNumbers(a , b):
  c = a + b
  return c

print ('Sum of 2 and 3 is %d' % (addNumbers(2 , 3)))
print (addNumbers(4 , 5 ))
print (addNumbers(2 , 3) * 4)

Thilan@ubuntu:~/programming$ python tmp1.py
Sum of 2 and 3 is 5
9
20

Thilan@ubuntu:~/programming$ python3 tmp1.py
Sum of 2 and 3 is 5
9
20
The procedure is same. But after function's instructions there is a command called return . It'll give some data out.What happen when we use a function as following? print (addNumbers(4 , 5 )) Actually function acts as a variable. Yeah the value of tht variable is depended on parameters those we supply to function. I think you got it clearly. Don't forget to share it. :-)