Hammed Babatunde
By Hammed Babatunde

Creating functions in Python

Creating functions in Python

What is a Function in Python

Formally, a function is a useful device that groups together a set of statements so they can be run more than once. They can also let us specify parameters that can serve as inputs to the functions.

On a more fundamental level, functions allow us to not have to repeatedly write the same code again and again. If you remember back to the lessons on strings and lists, remember that we used a function len() to get the length of a string. Since checking the length of a sequence is a common task you would want to write a function that can do this repeatedly at command.

def Statements

Let’s see how to build out a function’s syntax in Python. It has the following form:

1
2
3
4
5
6
7
8
9
10
11
12
13
def name_of_function(arg1,arg2):

    '''

    This is where the function's Document String (docstring) goes

    '''

    # Do stuff here

    # Return desired result

Example 1: A simple print ‘hello’ function

1
2
3
4
5
6
7
8
9
10
11
def say_hello():

    print('hello')



#Call the Function

say_hello()

Example 2: A simple greeting function

Let’s write a function that greets people with their name.

1
2
3
4
5
6
7
8
9
def greeting(name):

    print('Hello %s' %(name))



greeting('Jose')  #calling the function

Using return

Let’s see an example that use a return statement. return allows a function to return a result that can then be stored as a variable, or used in whatever manner a user wants.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import math



def is_prime2(num):

    '''

    Better method of checking for primes. 

    '''

    if num % 2 == 0 and num > 2: 

        return False

    for i in range(3, int(math.sqrt(num)) + 1, 2):

        if num % i == 0:

            return False

    return True

Great! You should now have a basic understanding of creating your own functions to save yourself from repeatedly writing code!