# Create Function in Python with Example

434 views   3 months ago Python

### Python Functions

In this tutorial, we’ll show you how to write functions in Python. Due to its simple and easy-to-understand syntax, Python is an ideal language to start off as a programmer. If you’re new to the programming world and want to focus more on the program logic rather than the fuzzy language syntax then Python is your way to go. Make sure to stick with us till the end as we embark upon this exciting journey of exploring the creation of functions in Python.

### What are Functions in Programming?

What are functions? Do we already know about them? Well, for those having a mathematics background, the term seems to be familiar. The + operator is actually a function that operates on two input values. It is dependent on two expected input values to generate its result and deliver that as an output. For example: 2 + 3 = 5. In other words, the + operator has a certain set of defined rules and we can reuse it whenever we feel the need of implementing its use case. In this case, the “certain set of defined rules” are just adding up the two given numbers.

The concept of functions in programming is not different. In fact, it is exactly the same. We create functions and define a certain set of rules such that they can be reused according to the need of the program. The idea is to cluster the main logic in one place and reuse that logic by calling a function instead of re-writing the same logic again and again. This way, our code is readable, maintainable and free of redundancy.

I hope the idea of functions in programming is clear. Now that we understand about functions, let’s move straight to learning how we can create these functions in Python programming language.

### Creating Functions in Python

This section will discuss in detail about functions in Python with the help of example use cases.

### Syntax

Python provides an inbuilt keyword `def` to define a function. We can create functions using the following syntax:

``````def your_function_name():
# This section is called function's body. We write here the code logic which acts as a reusable set of defined rules.
``````

Using the above syntax, we can create a function in Python. Simple, no? In the above code snippet, `def` is the keyword that lets Python know that we are about to create a function and `your_function_name` is the name of our function. These two are followed by a set of open-closed parenthesis `()` and finally a colon `:` marks the end of the formal declaration of a function. We can also write some comma-separated words in the parenthesis which are called parameters/arguments of the function. If you’re confused about the parameters part then hold on for a bit until we explore the examples of functions in Python.

By default, every Python function returns a `None` value. This means that even if we don’t include a `return` statement in our function, the Python function still returns a value i.e. `None`.

Let’s look at the simplest example to understand the syntax of a function.

### Example #1

Look at the following code segment:

``````def greet():
print("Hey there! This is Emad. Welcome to LaravelCode.")``````

The above code implements a function by the name `greet` and just prints a simple greeting message `Hey there! This is Emad. Welcome to CodeZen`. Now whenever I wish to greet someone, I can just call this function instead of writing the long message again and again.

We can call the above function by writing `greet()`:

``````greet()
'Output: Hey there! This is Emad. Welcome to LaravelCode'``````

### Example #2

The previous example implemented a function which only printed the greeting message to the console. Now, let’s look at another example. This time a bit complex than the previous one.

``````def getNaturalNumbersUntil(ending_value):
natural_numbers_list = []
for number in range(ending_value):
natural_numbers_list.append(number + 1)
return natural_numbers_list``````

In the above example, you’ll notice that the parenthesis after the function name is not empty. These are called parameters. This means that the function `getNaturalNumbersUntil` expects a certain value to be passed to it which it uses in the function body to execute the logic, in this case, the `ending_value`. The function cannot work if we don’t provide this parameter to it.

Suppose that the `ending_value` is 10. The function returns a list of natural numbers from 1 till 10 as the output as shown below.

``````print(getNaturalNumbersUntil(10))
'Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]'``````

The function `getNaturalNumbersUntil` provides the ease of getting a list of natural numbers up until any ending point we like. We do not need to write the for loop logic again and again. We just have to change the value of parameter `ending_value` and we are good to go.

### Take the Quiz

Enough talking. It’s time to test our understandings so far by taking a short conceptual quiz.

``````def printActivity(time_of_the_day):
if time_of_the_day == "morning":
print("Work")
elif time_of_the_day == "afternoon":
print("Have Lunch")
elif time_of_the_day == "evening":
print("Drink Coffee")
else:
print("Sleep")``````

What will be the output of following statement?

``print(printActivity("morning"))``

This is a tricky question to gauge your understanding so far. Take a moment to think deeply. There is a catch which you might miss.

If your answer is the same as stated below then you’re absolutely correct!

``````Work'
None``````

If you’re wondering where that `None` came from, then remember we discussed that every Python function returns a `None` value by default. Our function `printActivity` did not return anything so it followed the default convention.

###### Author : Harsukh Makwana Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles