Jun 09, 2020

Functions in python programming

Functions are life savers. Yes they make our life easier. 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

The keyword we use to build a function is def. It stands for  define. After you define it you can use it in the program.

In following program I define a new function named myFunction and use it on in the next lines. 

#!/usr/bin/env python
def myFunction():
  print ('I was printed by a function')

#now we can call it anywhere

#lets call it again myFunction()

I save it as function.py and run with Python interpreter.  Here is the output of the program.

[email protected]:~/programming$ python3 functions.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. 

You can't use a function before use it. Lets assume you write a code like following.


#!/usr/bin/env python 


def hello(a , b):
        print ('Hello, world')

But it gives us a n error saying the function is not defined.

Traceback (most recent call last):                                                                                                                                    
  File "function.py", line 3, in                                                                                                                                   
    hello() NameError: name 'hello' is not defined


Functions with parameters

We can supply some  parameters to a function and the function will do some task on those supplied data. Think about following example.

#!/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)

When define the function we states this function needs two arguments. So when ever we use this function we have to supply those arguments.

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

Now here we have used a function called addNumbers. It takes two parameters as a and b. After it will 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, it does those instructions on supplied data.

What happen if we don't supply enough parameters 

[email protected]:~$ python3 function.py
Traceback (most recent call last):
File "function.py", line 6, in addNumbers(2) TypeError: addNumbers() missing 1 required positional argument: 'b'

As well as we get another error if we or supply more than required number. Here is what I got when I used 

[email protected]:~$ python3 function.py
Traceback (most recent call last):
File "function.py", line 6, in addNumbers(3, 5, 6) TypeError: addNumbers() takes 2 positional arguments but 3 were given

It is up to you to do experiments. Code and learn new things. When you get errors, try to understand what happened.

Also we can set values as default parameters as following code.

#!/usr/bin/env python 

def addNumbers(a , b= 2):
        print ('Sum of %d and %d is %d' %( a , b , a+ b))


Now if we supply both a and b parameters, function assign given values to those variables. But we can skip giving second argument because we have assigned a default value to variable b.

So if we don't give second value Python automatically assign default value 5 to it.

Now you may realize that  print() is also a function. It expect at least one argument as the string to print on the screen. 

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.

#!/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)

Here is the output.

[email protected]:~/programming$ python3 function.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 that variable is depended on parameters those we supply to function.

So this is all for this tutorial. See you again on next one.

Jun 09
Python Inputs and Outputs

Getting user inputs is an essential part of a program. In python we have several ways to get inputs....

Feb 15
Introduction to Assembly language

Assembly is a low level programming language. You already know that low level programming languages....

May 16
Stack architecture theory tutorial

The stack is an important concept in computer science. If you are planning to learn reverse....

Replying to 's comment Cancel reply