Functions are an invaluable part of particularly in large programs. As your code keeps increases, the functions, basically a group of related statements, breaks things into modular tasks. The functions inside a code will keep it more organized as it grows. Today, let’s talk about how to create functions in python and how to create a function in python as well. 

What is a function in Python?

To keep it simple, a function in python is a block of reusable code, which performs specific or related tasks. Functions are often very important in programming as they will uphold the modularity along with the reusability of the written code.

As you might already know, Python offers two types of functions.

  • Built-in functions.
  • User-defined functions.

Built-in functions in python

Python by default offers some useful and ready-to-use functions for your coding purposes. They are always functionally pre-defined. For example: format(), min(), max(), dict(), bool() and many moreā€¦

You can find the list of all in-built functions in python here.

User-defined functions in python

The functions which are created by the user to perform a specific task are termed user-defined functions in python. These functions will help you to break a large program into modular tasks.

For example:

def add_numbers(x,y):
   sum = x + y
   return sum

num1 = 5
num2 = 6

print("The sum is", add_numbers(num1, num2))

The above function is a user-defined function. This function will take 2 numbers, add them and returns the result (sum).

Syntax of the function in python

Till now, you get to know about functions in python and it’s types. Now, here, we are going to see the syntax of the function and let’s see how it works.

Syntax of a function:

def function_name(parameters):
	"""docstring/s"""
	statement/s

This is very simple syntax. You have to define a function name along with its parameters. It should be followed by related statements. The docstrings will tell you what the function will do. 

Creating a function of python

I hope by now, you are well equipped with information which gets you going in creating a function python.

Let’s create a simple function in python.

def let_me_greet(name):
    """This function will great the input name"""
    print("Hello "+name+", Have a good time!!!")

Well, we have just created a function which takes the input name and greet it with a message.

Okay, our function looks good. But, wait!!!

How to call this function?

Let’s see that here.

Calling a function in python

After you create a function, you can call it whenever and wherever necessary. Note that you can it by another function or even a terminal.

To call a function, all you need to do is to call the function by it’s name along with it’s parameters.

Let’s call our defined function here.

let_me_greet("HackAnons")
Hello HackAnons, Have a good time!!!

Wow! That’s it. You have done it.

You just created a function, called it and it worked! How cool it is?!

Function with return statement in python

A return statement is used to end the execution of the code. Any statements after the return statement will not be executed.

Note that the return statement without any expression will return none.

def fun(): 
    website = "HackAnons"
    x = 101   
    return [website, 101];   
    
list = fun()  
print(list) 
['HackAnons', 101]

You can see that the working of return statement in the above code. Also, you can observe how we called the function to return it as a list.

Wrapping Up – Create a function in python

Functions are a bunch of statements that will help to organize the program by breaking the code to achieve modularity along with reusability. I hope you get to know about how to create functions in python and its operations. That’s all for now. Happy Python!!!.