DEV Community

Cover image for Python Functions
Baransel
Baransel

Posted on

Python Functions

Sign up to my newsletter!.

What is Function?

Functions are simply a set of commands created to do a particular function. For example, the print() function contains certain commands to make the function that allows us to output to the screen.

Why Functions?

Now you have thought of something like this, is it okay if I don't use a function? Of course, you can not use it, but after a certain point, when the line of code increases, the complexity increases, and it becomes difficult to control. That's why we use functions.

Thus, instead of writing code each time for the functions we want to do, we create a function once, and call it and use it whenever we want to do it. Thus, we get rid of excess code complexity.

Briefly Functions;

  • Each of them does a certain job.
  • We can divide it into two functions that take a parameter or a function that does not.
  • The function of functions is to gather complex operations together and enable us to perform these operations in one step.
  • By using functions, we can collect operations consisting of one or more steps under a single name.

If we know what functions are and why we use them, let's learn how to use them.

Function Usage

There are two types of functions in Python, the first is the functions that come ready with Python, that is, the embedded functions, and the second is the functions we have created. Since functions like print() and type() come with Python, we only call them where we want to use them.

The general usage outline of the functions is as follows.

def function_name(parameter1, parameter2):
    """docstring"""
    statement(s)

function_name(parameter1, parameter2)
Enter fullscreen mode Exit fullscreen mode

We have seen the draft in general, now let's show it with an example. Let me create a simple function for this right away.

def output():
    """Prints `Baransel.dev Python Lessons`"""
    print("Baransel.dev Python Lessons")
Enter fullscreen mode Exit fullscreen mode

As you can see we have created our function. Now let's call our function, for this we just write the name of the function;

def output():
    """Prints `Baransel.dev Python Lessons`"""
    print("Baransel.dev Python Lessons")

output()

# Baransel.dev Python Lessons
Enter fullscreen mode Exit fullscreen mode

Since we called it once, it did this function once. The number of times we call it, the more it prints to the screen.

def output():
    """Prints `Baransel.dev Python Lessons`"""
    print("Baransel.dev Python Lessons")

output()
output()
output()

# Baransel.dev Python Lessons
# Baransel.dev Python Lessons
# Baransel.dev Python Lessons
Enter fullscreen mode Exit fullscreen mode

If you wish, let's write the same function in a different way. I will use the return statement here because we will write functions that will return us results, not just printing to the screen, so we will use return.

return Statement

Continue this post on my blog! Python Functions.

Sign up to my newsletter!.

Top comments (1)

Collapse
 
hesoyamm profile image
Kishore kunal

I saw your blog, I hope you could also add about
if name == “main

This will give people more indepth understanding of how python executes the code.