# Functions | Python programming

## HacksLand | The computer science playground

Posted by Thilan Dissanayaka on Aug 12, 2019
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.
``````[email protected]:~/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()

[email protected]:~/programming\$ python tmp1.py
I was printed by a function
I was printed by a function

[email protected]:~/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.
``````[email protected]:~/programming\$ cat tmp1.py
#!/usr/bin/env python
print ('Sum of %d and %d is %d' %( a , b , a+ b))

[email protected]:~/programming\$ python tmp1.py
Sum of 2 and 4 is 6
Sum of 3 and 5 is 8

[email protected]:~/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.
``````[email protected]:~/programming\$ cat tmp1.py
#!/usr/bin/env python
c = a + b
return c

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

[email protected]:~/programming\$ python tmp1.py
Sum of 2 and 3 is 5
9
20

[email protected]:~/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. :-)

Hi, I'm Thilan. An engineering student from SriLanka. I love to code with Python, JavaScript PHP and C.