Home » Blog » How to Define a function in Python in just 3 Steps

# How to Define a function in Python in just 3 Steps

• Python, Web Dev

In today’s Article we will Define a function in Python, discuss about the purpose of return keyword, and the functions of Default and Variable Length Arguments.

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

Just like Python’s built-in functions such as print(), input() and more, you can build your own Python functions

## Define a function in Python:

We use the Def keyword to Define a Function in Python

``````def():
print("A Simple Function")``````

Any input arguments should be placed within these parentheses, this is called Parameters

``````def(x):
print(f"A Simple Function that inputs {x}")``````

## Syntax to Define a function in Python:

``````def add(x,y):
print(x+y)``````

Here the add() function takes in 2 Parameters (integers)

It prints out the sum of x and y

## Return:

Notice that we used `print()`, which is basically a built-in function inside your function

This is not quite ideal

That’s why we have the return keyword, and it returns a value for output. A return statement with no arguments is the same as return None.

``````def add(x,y):
return x+y
``12``

This is the same code as the previous, but using the return keyword

## Default arguments:

A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument.

``````def add(x=0, y=0):
return x+y
``0``

Here we’ve not defined any parameter for the function, and we set the default value to 0

## Variable-length arguments:

In our previous program we took 2 numbers and returned their sum, what if we enter three or more values? We get an error

Then is there a way to find the sum of variable number of integers? Yes, there is, by using Variable-length arguments.

You may need to process a function for more arguments than you specified while defining the function. These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments.

``````def add(x, *args):
for i in args:
x+=i
return x

``````15
30
6``````

## Examples where we Define a function in Python:

### Paragraph Length Checking Function:

``````def lencheck(para):
if len(para) <300 and len(para) >50:
return "Right Length"
elif len(para)<50:
return "Too Less"
elif len(para)>300:
return "That's alot"

lencheck("This paragraph contains very less content")``````
``Too Less``

### Is it an Exclamation Function:

``````def isitexclamation(msg):
if '!' in msg:
return True
else:
return False

isitexclamation("Geekalgo is Cool!")``````
``True``

## Conclusion:

In today’s post we’ve discussed how to Define a function in Python, purpose of return, about Default and Variable Length Arguments. If you are on your way to become a Pythonista, you might find our site really helpful. Hit this link for more python related posts
Check it out, it’s Simply the Best Code.